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.

338 lines
12 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. # 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 nemesis_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)
  18. MS = "-1" # 6 Product_MS_Classification (Microsoft Security)
  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. image = "-1" # 19 Product_Image
  32. vendor_image = "-1" # 20 Vendor_Image
  33. # find vendor name
  34. vendor = soup.find('div', {'class': 'd-flex align-items-center mt-n1'}).find('a').text
  35. vendor = cleanString(vendor).strip()
  36. # find product name
  37. name = soup.find('div', {'class': 'd-flex w-100 mt-4'}).find('a').text
  38. name = name.replace(r'[^a-zA-Z0-9]', '').strip()
  39. # find product description
  40. describe = soup.find('div', {'class': 'card card-bordered rounded-1 my-6'})\
  41. .find('div', {'class': 'fs-5 text-gray-800'}).text
  42. if describe is not None:
  43. describe = cleanString(describe).strip()
  44. else:
  45. describe = soup.find('div', {'class': 'card card-bordered rounded-1 my-6'}) \
  46. .find('div', {'class': 'fs-5 text-gray-800 mt-5'}).text
  47. if describe is not None:
  48. describe = cleanString(describe).strip()
  49. else:
  50. describe = "-1"
  51. # find product category
  52. temp = soup.find('div', {'class': 'fs-7 py-1'}).findAll('a')
  53. category = temp[1].text
  54. if category is not None:
  55. category = category.strip()
  56. else:
  57. category='-1'
  58. print('inside category')
  59. # all in class
  60. temp = soup.find('div', {'class': 'ms-4'}).findAll('div')
  61. # number of reviews
  62. reviews = temp[1].text
  63. if reviews is not None:
  64. reviews = reviews.replace("Reviews: ","").strip()
  65. else:
  66. reviews='-1'
  67. print('in rev')
  68. # item rating out of 5
  69. rating_item = temp[0].text
  70. if rating_item is None:
  71. rating_item = rating_item.replace("Rating: ","").replace(" out of 5","").strip()
  72. else:
  73. rating_item='-1'
  74. # amount of item sold
  75. success = temp[2].text
  76. if success is not None:
  77. success = success.replace('Sales: ', '').strip()
  78. else:
  79. success='-1'
  80. print('success')
  81. # amount
  82. temp = soup.find('div', {'class': 'text-gray-800 fs-1 fw-bolder mt-6'}).text
  83. if 'USD' in temp:
  84. USD = temp.replace('USD', '').strip()
  85. elif 'EUR' in temp:
  86. EURO = temp.replace('EUR', '').strip()
  87. # Finding Vendor Image
  88. vendor_image = soup.find('div', {"class": 'col'}).find('img')
  89. if vendor_image is not None:
  90. vendor_image = vendor_image.get('src')
  91. vendor_image = vendor_image.split('base64,')[-1]
  92. else:
  93. vendor_image = "-1"
  94. # Finding Product Image
  95. image = soup.find('div', {"class": 'container p-0'}).find('img')
  96. if image is not None:
  97. image = image.get('src')
  98. image = image.split('base64,')[-1]
  99. else:
  100. image = "-1"
  101. # Searching for CVE and MS categories
  102. cve = soup.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  103. if cve:
  104. CVE = " "
  105. for idx in cve:
  106. CVE += (idx)
  107. CVE += " "
  108. CVE = CVE.replace(',', ' ')
  109. CVE = CVE.replace('\n', '')
  110. ms = soup.findAll(text=re.compile('MS\d{2}-\d{3}'))
  111. if ms:
  112. MS = " "
  113. for im in ms:
  114. MS += (im)
  115. MS += " "
  116. MS = MS.replace(',', ' ')
  117. MS = MS.replace('\n', '')
  118. # Populating the final variable (this should be a list with all fields scraped)
  119. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  120. BTC, USD, EURO, sold, left, shipFrom, shipTo, image, vendor_image)
  121. # Sending the results
  122. return row
  123. # parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  124. # stores info it needs in different lists, these lists are returned after being organized
  125. # @param: soup object looking at html page of listing page
  126. # return: 'row' that contains a variety of lists that each hold info on the listing page
  127. def nemesis_listing_parser(soup):
  128. # Fields to be parsed
  129. nm = 0 # *Total_Products (Should be Integer)
  130. mktName = "NemesisMarket" # 0 *Marketplace_Name
  131. vendor = [] # 1 *Vendor y
  132. rating_vendor = [] # 2 Vendor_Rating
  133. success = [] # 3 Vendor_Successful_Transactions
  134. name = [] # 4 *Product_Name y
  135. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about this
  136. MS = [] # 6 Product_MS_Classification (Microsoft Security) dont worry about this
  137. category = [] # 7 Product_Category y
  138. describe = [] # 8 Product_Description
  139. views = [] # 9 Product_Number_Of_Views
  140. reviews = [] # 10 Product_Number_Of_Reviews
  141. rating_item = [] # 11 Product_Rating
  142. addDate = [] # 12 Product_AddDate
  143. BTC = [] # 13 Product_BTC_SellingPrice
  144. USD = [] # 14 Product_USD_SellingPrice y
  145. EURO = [] # 15 Product_EURO_SellingPrice
  146. sold = [] # 16 Product_QuantitySold
  147. qLeft = [] # 17 Product_QuantityLeft
  148. shipFrom = [] # 18 Product_ShippedFrom
  149. shipTo = [] # 19 Product_ShippedTo
  150. image = [] # 20 Product_Image
  151. image_vendor = [] # 21 Vendor_Image
  152. href = [] # 22 Product_Links
  153. cat = soup.find('span', {"class": "text-gray-700 fw-bold fs-2"}).text
  154. cat = cleanString(cat).strip()
  155. listing = soup.find('div', {"class": 'row g-5 g-xl-5'}).findAll('div', {"class": 'col-sm-6 col-md-4 col-lg-3 col-xxl-2'})
  156. # Populating the Number of Products
  157. nm = len(listing)
  158. for a in listing:
  159. category.append(cat)
  160. # Adding the url to the list of urls
  161. link = a.find('a').get('href')
  162. link = cleanLink(link)
  163. href.append(link)
  164. # Finding the Product name
  165. product = a.find('div', {"class": 'pt-3 pb-4 px-4'}).find('a').text.strip()
  166. # product = product.replace(r'[^a-zA-Z0-9]', '').strip()
  167. name.append(product)
  168. # Finding Product Image
  169. product_image = a.find('img')
  170. if product_image is not None:
  171. product_image = product_image.get('src')
  172. product_image = product_image.split('base64,')[-1]
  173. image.append(product_image)
  174. else:
  175. image.append("-1")
  176. # Finding Prices
  177. price = a.find('div', {"class": "fs-4 text-gray-800 fw-bolder mt-3"}).text
  178. price = price.strip()
  179. if 'USD' in price:
  180. price = price.replace('USD', '')
  181. price = price.strip()
  182. USD.append(price)
  183. EURO.append("-1")
  184. elif 'EUR' in price:
  185. price = price.replace('EUR', '')
  186. price = price.strip()
  187. EURO.append(price)
  188. USD.append("-1")
  189. else:
  190. USD.append("-1")
  191. EURO.append('-1')
  192. # Finding Item Rating
  193. temp = a.find('div', {"class": "d-flex flex-column"}).findAll('span', {"class": "badge badge-sm badge-light-dark text-gray-800 rounded-1"})
  194. rating = temp[0].text.strip()
  195. if rating is not None:
  196. rating_item.append(rating)
  197. else:
  198. rating_item.append('-1')
  199. print('rating')
  200. # find number of reviews
  201. rev = temp[1].text.strip()
  202. if rev is not None:
  203. reviews.append(rev)
  204. else:
  205. reviews.append('-1')
  206. print('reviews')
  207. # sales
  208. sales = temp[2].text.strip()
  209. if sales is not None:
  210. success.append(sales)
  211. else:
  212. success.append('-1')
  213. print('success')
  214. # Finding the Vendor
  215. vendor_name = a.find('div', {"class": "d-flex flex-column"}).find('a').text
  216. vendor_name = vendor_name.replace(",", "")
  217. vendor_name = vendor_name.strip()
  218. vendor.append(vendor_name)
  219. # vendor image
  220. vendor_image = a.find('div', {"class": 'd-flex align-items-center'}).find('a').find('img')
  221. if vendor_image is not None:
  222. vendor_image = vendor_image.get('src')
  223. vendor_image = vendor_image.split('base64,')[-1]
  224. image_vendor.append(vendor_image)
  225. else:
  226. image_vendor.append("-1")
  227. # item description
  228. description = a.find('div', {"class": 'fs-5 text-gray-800 mt-2'}).text
  229. if description is not None:
  230. description = description.replace('\n', ' ')
  231. describe.append(description)
  232. else:
  233. describe.append('-1')
  234. print('describe')
  235. # Searching for CVE and MS categories
  236. cve = a.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  237. if not cve:
  238. cveValue = "-1"
  239. else:
  240. cee = " "
  241. for idx in cve:
  242. cee += (idx)
  243. cee += " "
  244. cee = cee.replace(',', ' ')
  245. cee = cee.replace('\n', '')
  246. cveValue = cee
  247. CVE.append(cveValue)
  248. ms = a.findAll(text=re.compile('MS\d{2}-\d{3}'))
  249. if not ms:
  250. MSValue = "-1"
  251. else:
  252. me = " "
  253. for im in ms:
  254. me += (im)
  255. me += " "
  256. me = me.replace(',', ' ')
  257. me = me.replace('\n', '')
  258. MSValue = me
  259. MS.append(MSValue)
  260. val="-1"
  261. sold.append(val)
  262. views.append(val)
  263. addDate.append(val)
  264. BTC.append(val)
  265. qLeft.append(val)
  266. shipFrom.append(val)
  267. shipTo.append(val)
  268. rating_vendor.append(val)
  269. # Populate the final variable (this should be a list with all fields scraped)
  270. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  271. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href, image, image_vendor)
  272. # called by the crawler to get description links on a listing page
  273. # @param: beautifulsoup object that is using the correct html page (listing page)
  274. # return: list of description links from a listing page
  275. def nemesis_links_parser(soup):
  276. # Returning all links that should be visited by the Crawler
  277. href = []
  278. listing = soup.findAll('div', {"class": "col-sm-6 col-md-4 col-lg-3 col-xxl-2"})
  279. for a in listing:
  280. bae = a.findAll('a', href=True)
  281. # Adding the url to the list of urls
  282. link = bae[0].get('href')
  283. href.append(link)
  284. return href