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.

280 lines
9.2 KiB

  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. # This is the method to parse the Description Pages (one page to each Product in the Listing Pages)
  7. def hiddenmarket_description_parser(soup):
  8. # Fields to be parsed
  9. vendor = "-1" # 0 *Vendor_Name
  10. success = "-1" # 1 Vendor_Successful_Transactions
  11. rating_vendor = "-1" # 2 Vendor_Rating
  12. name = "-1" # 3 *Product_Name
  13. describe = "-1" # 4 Product_Description
  14. CVE = "-1" # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  15. MS = "-1" # 6 Product_MS_Classification (Microsoft Security)
  16. category = "-1" # 7 Product_Category
  17. views = "-1" # 8 Product_Number_Of_Views
  18. reviews = "-1" # 9 Product_Number_Of_Reviews
  19. rating_item = "-1" # 10 Product_Rating
  20. addDate = "-1" # 11 Product_AddedDate
  21. BTC = "-1" # 12 Product_BTC_SellingPrice
  22. USD = "-1" # 13 Product_USD_SellingPrice
  23. EURO = "-1" # 14 Product_EURO_SellingPrice
  24. sold = "-1" # 15 Product_QuantitySold
  25. left = "-1" # 16 Product_QuantityLeft
  26. shipFrom = "-1" # 17 Product_ShippedFrom
  27. shipTo = "-1" # 18 Product_ShippedTo
  28. bae = soup.find('div', {'class': "main"})
  29. # Finding Product Name
  30. name = bae.find('div', {'class': "heading"}).text
  31. name = name.replace('\n', ' ')
  32. name = name.replace(",", "")
  33. name = name.strip()
  34. mb = bae.find('div', {'class': "information"}).findAll('tr')
  35. # Finding Vendor
  36. vendor = mb[1].find('a').text
  37. vendor = vendor.replace(",", "")
  38. vendor = vendor.strip()
  39. # # Finding Vendor Rating
  40. # full_stars = bae[2].find_all('i', {'class': "fas fa-star"})
  41. # half_star = bae[2].find('i', {'class': "fas fa-star-half-alt"})
  42. # rating = len(full_stars) + (0.5 if half_star is not None else 0)
  43. # Finding Quantity Left
  44. temp = mb[-3].text
  45. left = temp.replace("Quantity in stock:", "")
  46. left = left.strip()
  47. # Finding USD
  48. USD = mb[0].text
  49. USD = USD.replace("Price:", "")
  50. USD = USD.replace("USD", "")
  51. USD = USD.strip()
  52. # Finding BTC
  53. # temp = bae.find('div', {"class": "small"}).text.split("BTC")
  54. # BTC = temp[0].strip()
  55. # Finding Shipment Information (Origin)
  56. shipFrom = mb[2].text
  57. shipFrom = shipFrom.replace("Seller location:", "")
  58. shipFrom = shipFrom.strip()
  59. # Finding Shipment Information (Destination)
  60. shipTo = mb[3].text
  61. shipTo = shipTo.replace("Ships to (seller):", "")
  62. shipTo = shipTo.strip()
  63. # Finding the Product description
  64. describe = bae.find('div', {"class": "twotabs"}).find('div', {'class': "tab1"}).text
  65. describe = describe.replace("\n", " ")
  66. describe = describe.replace("\r", " ")
  67. describe = describe.replace("-", " ")
  68. describe = describe.strip()
  69. # Finding the Product Category
  70. category = mb[-4].text
  71. category = category.replace("Category:", "")
  72. category = category.strip()
  73. #Finding the number of reviews
  74. reviews = bae.find_all('div', {'class': "heading"})
  75. reviews = reviews[-2].text
  76. reviews = reviews.replace("Comments (", "")
  77. reviews = reviews.replace(")", "")
  78. # Searching for CVE and MS categories
  79. cve = soup.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  80. if cve:
  81. CVE = " "
  82. for idx in cve:
  83. CVE += (idx)
  84. CVE += " "
  85. CVE = CVE.replace(',', ' ')
  86. CVE = CVE.replace('\n', '')
  87. ms = soup.findAll(text=re.compile('MS\d{2}-\d{3}'))
  88. if ms:
  89. MS = " "
  90. for im in ms:
  91. MS += (im)
  92. MS += " "
  93. MS = MS.replace(',', ' ')
  94. MS = MS.replace('\n', '')
  95. # Populating the final variable (this should be a list with all fields scraped)
  96. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  97. BTC, USD, EURO, sold, left, shipFrom, shipTo)
  98. # Sending the results
  99. return row
  100. # This is the method to parse the Listing Pages
  101. def hiddenmarket_listing_parser(soup):
  102. # Fields to be parsed
  103. nm = 0 # *Total_Products (Should be Integer)
  104. mktName = "HiddenMarket" # 0 *Marketplace_Name
  105. vendor = [] # 1 *Vendor y
  106. rating_vendor = [] # 2 Vendor_Rating
  107. success = [] # 3 Vendor_Successful_Transactions
  108. name = [] # 4 *Product_Name y
  109. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  110. MS = [] # 6 Product_MS_Classification (Microsoft Security)
  111. category = [] # 7 Product_Category y
  112. describe = [] # 8 Product_Description
  113. views = [] # 9 Product_Number_Of_Views
  114. reviews = [] # 10 Product_Number_Of_Reviews
  115. rating_item = [] # 11 Product_Rating
  116. addDate = [] # 12 Product_AddDate
  117. BTC = [] # 13 Product_BTC_SellingPrice
  118. USD = [] # 14 Product_USD_SellingPrice y
  119. EURO = [] # 15 Product_EURO_SellingPrice
  120. sold = [] # 16 Product_QuantitySold
  121. qLeft = [] # 17 Product_QuantityLeft
  122. shipFrom = [] # 18 Product_ShippedFrom
  123. shipTo = [] # 19 Product_ShippedTo
  124. href = [] # 20 Product_Links
  125. listing = soup.findAll('div', {"class": "item"})
  126. # Populating the Number of Products
  127. nm = len(listing)
  128. # Finding Category
  129. # cat = soup.find("div", {'class': "heading"}).text
  130. # cat = cat.replace(",", "")
  131. # cat = cat.strip()
  132. for card in listing:
  133. # category.append(cat)
  134. # Adding the url to the list of urls
  135. link = card.find_all('a')
  136. link = link[1].get('href')
  137. href.append(link)
  138. # Finding Product Name
  139. product = card.next_sibling.find('div', {'class': "title"})
  140. product = product.text
  141. product = product.replace('\n', ' ')
  142. product = product.replace(",", "")
  143. product = product.strip()
  144. name.append(product)
  145. # Finding Vendor
  146. vendor_name = card.text
  147. vendor_name = vendor_name.replace(",", "")
  148. vendor_name = vendor_name.strip()
  149. vendor.append(vendor_name)
  150. # Finding USD
  151. usd = card.next_sibling.find('div', {"class": "buttons"}).find('div', {'class': "price"}).text
  152. usd = usd.replace("USD", "")
  153. usd = usd.strip()
  154. USD.append(usd)
  155. tb = card.next_sibling.find("div", {"class": "stats"})
  156. tb = tb.find_all('td')
  157. # Finding Reviews
  158. num = tb[-1].text
  159. num = num.strip()
  160. reviews.append(num)
  161. # Finding Views
  162. view = tb[-3].text.strip()
  163. views.append(view)
  164. # Finding Num of Sales
  165. sale = tb[-2].text.strip()
  166. sold.append(sale)
  167. # Finding Item Rating
  168. if num == '0':
  169. item_rating = '-1'
  170. else:
  171. item_rating = card.next_sibling.find('div', {'class': 'stats'}).find('div', {'class': "stars2"})
  172. item_rating = item_rating.get('style')
  173. item_rating = item_rating.replace("width:", "")
  174. item_rating = item_rating.replace("%", "")
  175. item_rating = (float(item_rating) * 5.0) / 100.0
  176. item_rating = "{:.{}f}".format(item_rating, 2)
  177. rating_item.append(item_rating)
  178. # Finding shipping info
  179. shipping = card.next_sibling.find('div', {'class': "shipping"}).text.split('>')
  180. # SHip from
  181. origin = shipping[0].strip()
  182. shipFrom.append(origin)
  183. #Ship to
  184. destination = shipping[1].strip()
  185. shipTo.append(destination)
  186. # Finding description (site only shows partial description on listing pages)
  187. # description = card.next_sibling.find('div', {'class': "description"}).text
  188. # description = description.replace("\n", " ")
  189. # description = description.replace("\r", " ")
  190. # description = description.replace("-", " ")
  191. # description = description.strip()
  192. # describe.append(description)
  193. # Searching for CVE and MS categories
  194. cve = card.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  195. if not cve:
  196. cveValue = "-1"
  197. else:
  198. cee = " "
  199. for idx in cve:
  200. cee += (idx)
  201. cee += " "
  202. cee = cee.replace(',', ' ')
  203. cee = cee.replace('\n', '')
  204. cveValue = cee
  205. CVE.append(cveValue)
  206. ms = card.findAll(text=re.compile('MS\d{2}-\d{3}'))
  207. if not ms:
  208. MSValue = "-1"
  209. else:
  210. me = " "
  211. for im in ms:
  212. me += (im)
  213. me += " "
  214. me = me.replace(',', ' ')
  215. me = me.replace('\n', '')
  216. MSValue = me
  217. MS.append(MSValue)
  218. # Populate the final variable (this should be a list with all fields scraped)
  219. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  220. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href)
  221. def hiddenmarket_links_parser(soup):
  222. # Returning all links that should be visited by the Crawler
  223. href = []
  224. listing = soup.findAll('div', {"class": "item"})
  225. for div in listing:
  226. link = div.findAll('a')
  227. link = link[1]
  228. link = link['href']
  229. href.append(link)
  230. return href