this is based on calsyslab project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
9.6 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. __author__ = 'DarkWeb'
  2. # Here, we are importing the auxiliary functions to clean or convert data
  3. from MarketPlaces.Utilities.utilities import *
  4. # Here, we are importing BeautifulSoup to search through the HTML tree
  5. from bs4 import BeautifulSoup
  6. #parses description pages, so takes html pages of description pages using soup object, and parses it for info it needs
  7. #stores info it needs in different lists, these lists are returned after being organized
  8. #@param: soup object looking at html page of description page
  9. #return: 'row' that contains a variety of lists that each hold info on the description page
  10. def torbay_description_parser(soup):
  11. # Fields to be parsed
  12. vendor = "-1" # 0 *Vendor_Name
  13. success = "-1" # 1 Vendor_Successful_Transactions
  14. rating_vendor = "-1" # 2 Vendor_Rating
  15. name = "-1" # 3 *Product_Name
  16. describe = "-1" # 4 Product_Description
  17. CVE = "-1" # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about that much
  18. MS = "-1" # 6 Product_MS_Classification (Microsoft Security) dont worry about that much
  19. category = "-1" # 7 Product_Category
  20. views = "-1" # 8 Product_Number_Of_Views
  21. reviews = "-1" # 9 Product_Number_Of_Reviews
  22. rating_item = "-1" # 10 Product_Rating
  23. addDate = "-1" # 11 Product_AddedDate
  24. BTC = "-1" # 12 Product_BTC_SellingPrice
  25. USD = "-1" # 13 Product_USD_SellingPrice
  26. EURO = "-1" # 14 Product_EURO_SellingPrice
  27. sold = "-1" # 15 Product_QuantitySold
  28. left = "-1" # 16 Product_QuantityLeft
  29. shipFrom = "-1" # 17 Product_ShippedFrom
  30. shipTo = "-1" # 18 Product_ShippedTo
  31. # Finding Product Name
  32. name = soup.find('div', {'class': 'product-information'}).find('h1').text.strip()
  33. # Finding Vendor
  34. vendor = soup.find('div', {"class": "profile-info"}).find('a').text.strip()
  35. # Finding Vendor Rating
  36. rating_vendor.append(-1)
  37. # Finding Successful Transactions
  38. success.append(-1)
  39. bae = soup.find('div', {'class': "box"}).find_all('ul')
  40. # Finding Prices
  41. USD = soup.find('div', {'class': "total-price"}).find('span').text.strip()
  42. # Finding the Product Category
  43. category = soup.find('div', {'class': "profile-info"}).find('p').find('a').text.strip()
  44. # Finding the Product Quantity Available
  45. left.append(-1)
  46. # Finding Number Sold
  47. sold.append(-1)
  48. li = bae[3].find_all('li')
  49. # Finding Shipment Information (Origin)
  50. if "Ships from:" in li[-2].text:
  51. shipFrom = li[-2].text
  52. shipFrom = shipFrom.replace("Ships from: ", "")
  53. # shipFrom = shipFrom.replace(",", "")
  54. shipFrom = shipFrom.strip()
  55. # Finding Shipment Information (Destination)
  56. shipTo = li[-1].find('div', {'title': "List of countries is scrollable"}).text
  57. shipTo = shipTo.replace("Ships to: ", "")
  58. shipTo = shipTo.strip()
  59. if "certain countries" in shipTo:
  60. countries = ""
  61. tags = li[-1].find_all('span', {'class': "tag"})
  62. for tag in tags:
  63. country = tag.text.strip()
  64. countries += country + ", "
  65. shipTo = countries.strip(", ")
  66. # Finding the Product description
  67. describe = soup.find('div', {'class': "pre-line"}).text
  68. describe = describe.replace("\n", " ")
  69. describe = describe.strip()
  70. '''# Finding the Number of Product Reviews
  71. tag = soup.findAll(text=re.compile('Reviews'))
  72. for index in tag:
  73. reviews = index
  74. par = reviews.find('(')
  75. if par >=0:
  76. reviews = reviews.replace("Reviews (","")
  77. reviews = reviews.replace(")","")
  78. reviews = reviews.split(",")
  79. review = str(abs(int(reviews[0])) + abs(int(reviews[1])))
  80. else :
  81. review = "-1"'''
  82. # Searching for CVE and MS categories
  83. cve = soup.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  84. if cve:
  85. CVE = " "
  86. for idx in cve:
  87. CVE += (idx)
  88. CVE += " "
  89. CVE = CVE.replace(',', ' ')
  90. CVE = CVE.replace('\n', '')
  91. ms = soup.findAll(text=re.compile('MS\d{2}-\d{3}'))
  92. if ms:
  93. MS = " "
  94. for im in ms:
  95. MS += (im)
  96. MS += " "
  97. MS = MS.replace(',', ' ')
  98. MS = MS.replace('\n', '')
  99. # Populating the final variable (this should be a list with all fields scraped)
  100. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  101. BTC, USD, EURO, sold, left, shipFrom, shipTo)
  102. # Sending the results
  103. return row
  104. #parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  105. #stores info it needs in different lists, these lists are returned after being organized
  106. #@param: soup object looking at html page of listing page
  107. #return: 'row' that contains a variety of lists that each hold info on the listing page
  108. def torbay_listing_parser(soup):
  109. # Fields to be parsed
  110. nm = 0 # *Total_Products (Should be Integer)
  111. mktName = "TorBay" # 0 *Marketplace_Name
  112. vendor = [] # 1 *Vendor y
  113. rating_vendor = [] # 2 Vendor_Rating
  114. success = [] # 3 Vendor_Successful_Transactions
  115. name = [] # 4 *Product_Name y
  116. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about this
  117. MS = [] # 6 Product_MS_Classification (Microsoft Security) dont worry about this
  118. category = [] # 7 Product_Category y
  119. describe = [] # 8 Product_Description
  120. views = [] # 9 Product_Number_Of_Views
  121. reviews = [] # 10 Product_Number_Of_Reviews
  122. rating_item = [] # 11 Product_Rating
  123. addDate = [] # 12 Product_AddDate
  124. BTC = [] # 13 Product_BTC_SellingPrice
  125. USD = [] # 14 Product_USD_SellingPrice y
  126. EURO = [] # 15 Product_EURO_SellingPrice
  127. sold = [] # 16 Product_QuantitySold
  128. qLeft = [] # 17 Product_QuantityLeft
  129. shipFrom = [] # 18 Product_ShippedFrom
  130. shipTo = [] # 19 Product_ShippedTo
  131. href = [] # 20 Product_Links
  132. listing = soup.findAll('div', {"class": "product-card"})
  133. # Populating the Number of Products
  134. nm = len(listing)
  135. for a in listing:
  136. bae = a.findAll('a', href=True)
  137. # Adding the url to the list of urls
  138. link = bae[0].get('href')
  139. link = cleanLink(link)
  140. href.append(link)
  141. # Finding the Product
  142. product = bae[1].find('p').text
  143. product = product.replace('\n', ' ')
  144. product = product.replace(",", "")
  145. product = product.replace("...", "")
  146. product = product.strip()
  147. name.append(product)
  148. bae = a.find('div', {'class': "media-content"}).find('div').find_all('div')
  149. if len(bae) >= 5:
  150. # Finding Prices
  151. price = bae[0].text
  152. ud = price.replace(" USD", " ")
  153. # u = ud.replace("$","")
  154. u = ud.replace(",", "")
  155. u = u.strip()
  156. USD.append(u)
  157. # bc = (prc[1]).strip(' BTC')
  158. # BTC.append(bc)
  159. # Finding the Vendor
  160. vendor_name = bae[1].find('a').text
  161. vendor_name = vendor_name.replace(",", "")
  162. vendor_name = vendor_name.strip()
  163. vendor.append(vendor_name)
  164. # Finding the Category
  165. cat = bae[2].find('small').text
  166. cat = cat.replace("Category: ", "")
  167. cat = cat.replace(",", "")
  168. cat = cat.strip()
  169. category.append(cat)
  170. # Finding Number Sold and Quantity Left
  171. num = bae[3].text
  172. num = num.replace("Sold: ", "")
  173. num = num.strip()
  174. sold.append(num)
  175. quant = bae[4].find('small').text
  176. quant = quant.replace("In stock: ", "")
  177. quant = quant.strip()
  178. qLeft.append(quant)
  179. # Finding Successful Transactions
  180. freq = bae[1].text
  181. freq = freq.replace(vendor_name, "")
  182. freq = re.sub(r'Vendor Level \d+', "", freq)
  183. freq = freq.replace("(", "")
  184. freq = freq.replace(")", "")
  185. freq = freq.strip()
  186. success.append(freq)
  187. # Searching for CVE and MS categories
  188. cve = a.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  189. if not cve:
  190. cveValue="-1"
  191. else:
  192. cee = " "
  193. for idx in cve:
  194. cee += (idx)
  195. cee += " "
  196. cee = cee.replace(',', ' ')
  197. cee = cee.replace('\n', '')
  198. cveValue=cee
  199. CVE.append(cveValue)
  200. ms = a.findAll(text=re.compile('MS\d{2}-\d{3}'))
  201. if not ms:
  202. MSValue="-1"
  203. else:
  204. me = " "
  205. for im in ms:
  206. me += (im)
  207. me += " "
  208. me = me.replace(',', ' ')
  209. me = me.replace('\n', '')
  210. MSValue=me
  211. MS.append(MSValue)
  212. # Populate the final variable (this should be a list with all fields scraped)
  213. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  214. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href)
  215. #called by the crawler to get description links on a listing page
  216. #@param: beautifulsoup object that is using the correct html page (listing page)
  217. #return: list of description links from a listing page
  218. def torbay_links_parser(soup):
  219. # Returning all links that should be visited by the Crawler
  220. href = []
  221. listing = soup.find('section', {"id": "content"}).findAll('div', {"class": "product-card"})
  222. for a in listing:
  223. bae = a.find('div', {"class": "pc-footer"}).find('a', {"class": "btn btn-primary"}, href=True)
  224. link = bae['href']
  225. href.append(link)
  226. return href