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.

266 lines
9.3 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 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 darkbazar_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. # Finding Product Name
  32. divmb = soup.findAll('div', {'class': "mb-1"})
  33. name = divmb[0].text
  34. name = name.replace('\n', ' ')
  35. name = name.replace(",", "")
  36. name = name.strip()
  37. # Finding Vendor
  38. vendor = divmb[1].find('a').text.strip()
  39. # Finding Vendor Rating
  40. temp = soup.find('div', {'class': ""}).text
  41. temp = temp.split('(')
  42. rating = temp[0].replace("Vendor's Review : ", "")
  43. rating = rating.replace("%", "")
  44. rating_vendor = rating.strip()
  45. # Finding the Product Rating and Number of Product Reviews
  46. reviews = temp[2].replace(" review)", "")
  47. reviews = reviews.strip()
  48. temp = temp[1].split(")")
  49. rating = temp[1].replace("Product Review : ", "")
  50. rating = rating.replace("%", "")
  51. rating_item = rating.strip()
  52. # Finding Prices
  53. USD = soup.find('div', {'class': "h3 text-primary"}).text.strip()
  54. # Finding the Product Category
  55. pmb = soup.findAll('p', {'class': "mb-1"})
  56. category = pmb[-1].text
  57. category = category.replace("Category: ", "").strip()
  58. # Finding the Product Quantity Available
  59. left = divmb[-1].text
  60. left = left.split(",", 1)[1]
  61. left = left.replace("in stock", "")
  62. left = left.strip()
  63. # Finding Number Sold
  64. sold = divmb[-1].text
  65. sold = sold.split(",", 1)[0]
  66. sold = sold.replace("sold", "")
  67. sold = sold.strip()
  68. # Finding Shipment Information (Origin)
  69. pmb[0].text
  70. shipFrom = shipFrom.replace("Ships from: ", "").strip()
  71. # Finding Shipment Information (Destination)
  72. pmb[1].text
  73. shipTo = shipTo.replace("Ships to: ", "").strip()
  74. # Finding the Product description
  75. cardbody = soup.findAll('div', {'class': "card-body"})
  76. describe = cardbody[1].text.strip()
  77. # Searching for CVE and MS categories
  78. cve = soup.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  79. if cve:
  80. CVE = " "
  81. for idx in cve:
  82. CVE += (idx)
  83. CVE += " "
  84. CVE = CVE.replace(',', ' ')
  85. CVE = CVE.replace('\n', '')
  86. ms = soup.findAll(text=re.compile('MS\d{2}-\d{3}'))
  87. if ms:
  88. MS = " "
  89. for im in ms:
  90. MS += (im)
  91. MS += " "
  92. MS = MS.replace(',', ' ')
  93. MS = MS.replace('\n', '')
  94. # Populating the final variable (this should be a list with all fields scraped)
  95. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  96. BTC, USD, EURO, sold, left, shipFrom, shipTo)
  97. # Sending the results
  98. return row
  99. # parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  100. # stores info it needs in different lists, these lists are returned after being organized
  101. # @param: soup object looking at html page of listing page
  102. # return: 'row' that contains a variety of lists that each hold info on the listing page
  103. def darkbazar_listing_parser(soup):
  104. # Fields to be parsed
  105. nm = 0 # *Total_Products (Should be Integer)
  106. mktName = "DarkBazar" # 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) dont worry about this
  112. MS = [] # 6 Product_MS_Classification (Microsoft Security) dont worry about this
  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. href = [] # 20 Product_Links
  127. listing = soup.findAll('div', {"id": "itembox"})
  128. # Populating the Number of Products
  129. nm = len(listing)
  130. for a in listing:
  131. bae = a.findAll('a', href=True)
  132. lb = a.findAll('div', {"id": "littlebox"})
  133. # Adding the url to the list of urls
  134. link = bae[0].get('href')
  135. link = cleanLink(link)
  136. href.append(link)
  137. # Finding the Product
  138. product = lb[1].find('a').text
  139. product = product.replace('\n', ' ')
  140. product = product.replace(",", "")
  141. product = product.replace("...", "")
  142. product = product.strip()
  143. name.append(product)
  144. # Finding Prices
  145. price = lb[-1].find('div', {"class": "mb-1"}).text
  146. price = price.replace("$","")
  147. price = price.strip()
  148. USD.append(price)
  149. # Finding the Vendor
  150. vendor_name = lb[-1].find("a").text
  151. vendor_name = vendor_name.replace(",", "")
  152. vendor_name = vendor_name.strip()
  153. vendor.append(vendor_name)
  154. # Finding the Category
  155. cat = lb[-1].find("span").text
  156. cat = cat.replace("class:", "")
  157. cat = cat.strip()
  158. category.append(cat)
  159. span = lb[0].findAll("span")
  160. # Finding Number of Views
  161. num = span[0].text
  162. num = num.replace("views:", "")
  163. num = num.strip()
  164. sold.append(num)
  165. # Finding Number Sold
  166. num = span[2].text
  167. num = num.replace("Sold:", "")
  168. num = num.strip()
  169. sold.append(num)
  170. # Finding Quantity Left
  171. quant = span[1].text
  172. quant = quant.replace("stock:", "")
  173. quant = quant.strip()
  174. qLeft.append(quant)
  175. # Searching for CVE and MS categories
  176. cve = a.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  177. if not cve:
  178. cveValue = "-1"
  179. else:
  180. cee = " "
  181. for idx in cve:
  182. cee += (idx)
  183. cee += " "
  184. cee = cee.replace(',', ' ')
  185. cee = cee.replace('\n', '')
  186. cveValue = cee
  187. CVE.append(cveValue)
  188. ms = a.findAll(text=re.compile('MS\d{2}-\d{3}'))
  189. if not ms:
  190. MSValue = "-1"
  191. else:
  192. me = " "
  193. for im in ms:
  194. me += (im)
  195. me += " "
  196. me = me.replace(',', ' ')
  197. me = me.replace('\n', '')
  198. MSValue = me
  199. MS.append(MSValue)
  200. # Populate the final variable (this should be a list with all fields scraped)
  201. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  202. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href)
  203. # called by the crawler to get description links on a listing page
  204. # @param: beautifulsoup object that is using the correct html page (listing page)
  205. # return: list of description links from a listing page
  206. def darkbazar_links_parser(soup):
  207. # Returning all links that should be visited by the Crawler
  208. href = []
  209. listing = soup.findAll('div', {"id": "itembox"})
  210. # for a in listing:
  211. # bae = a.find('a', {"class": "text-info"}, href=True)
  212. # link = bae['href']
  213. # href.append(link)
  214. for a in listing:
  215. bae = a.findAll('a', href=True)
  216. # Adding the url to the list of urls
  217. link = bae[0].get('href')
  218. href.append(link)
  219. return href