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.

293 lines
9.6 KiB

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