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.

288 lines
9.4 KiB

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