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.

208 lines
8.5 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 darkbay_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. # vendor name
  34. vendor = soup.find('div', {'class', 'vendor'}).find('a').text
  35. # product name
  36. name = soup.find('h3').text
  37. # description
  38. describe = soup.find('div', {'class', 'description'}).text.replace('\n', ' ')
  39. # category
  40. category = soup.find('i', {'class', 'fas fa-folder'}).text
  41. # finding price
  42. money = soup.find('div', {'class', 'price'})
  43. BTC = money.find('span', {'class', 'btc'}).text.replace('$','')
  44. USD = money.find('span', {'class', 'usd'}).text.replace('','')
  45. if soup.find('div', {'class': 'instock many'}):
  46. left = soup.find('div', {'class': 'instock many'}).text
  47. if 'in stock' in left:
  48. left.replace('in stock', '').strip()
  49. if left == '':
  50. left = '-1'
  51. # ship to and ship from
  52. location = soup.find('dl', {'class': 'compact ships mb1'}).findAll('dd')
  53. shipFrom = location[0].text
  54. shipFrom = ''.join(c for c in shipFrom if c.isalnum())
  55. shipFrom = shipFrom.strip()
  56. shipFrom = cleanString(shipFrom)
  57. shipTo = location[1].text
  58. shipTo = ''.join(c for c in shipTo if c.isalnum())
  59. shipTo = shipTo.strip()
  60. # Finding Product Image
  61. if soup.find('div', {"class": 'product-photos'}):
  62. image = soup.find('div', {"class": 'product-photos'}).find('img')
  63. image = image.get('src')
  64. image = image.split('base64,')[-1]
  65. else:
  66. image = "-1"
  67. # Populating the final variable (this should be a list with all fields scraped)
  68. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  69. BTC, USD, EURO, sold, left, shipFrom, shipTo, image, vendor_image)
  70. # Sending the results
  71. return row
  72. # parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  73. # stores info it needs in different lists, these lists are returned after being organized
  74. # @param: soup object looking at html page of listing page
  75. # return: 'row' that contains a variety of lists that each hold info on the listing page
  76. def darkbay_listing_parser(soup):
  77. # Fields to be parsed
  78. nm = 0 # *Total_Products (Should be Integer)
  79. mktName = "DarkBay" # 0 *Marketplace_Name
  80. vendor = [] # 1 *Vendor y
  81. rating_vendor = [] # 2 Vendor_Rating
  82. success = [] # 3 Vendor_Successful_Transactions
  83. name = [] # 4 *Product_Name y
  84. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about this
  85. MS = [] # 6 Product_MS_Classification (Microsoft Security) dont worry about this
  86. category = [] # 7 Product_Category y
  87. describe = [] # 8 Product_Description
  88. views = [] # 9 Product_Number_Of_Views
  89. reviews = [] # 10 Product_Number_Of_Reviews
  90. rating_item = [] # 11 Product_Rating
  91. addDate = [] # 12 Product_AddDate
  92. BTC = [] # 13 Product_BTC_SellingPrice
  93. USD = [] # 14 Product_USD_SellingPrice y
  94. EURO = [] # 15 Product_EURO_SellingPrice
  95. sold = [] # 16 Product_QuantitySold
  96. qLeft = [] # 17 Product_QuantityLeft
  97. shipFrom = [] # 18 Product_ShippedFrom
  98. shipTo = [] # 19 Product_ShippedTo
  99. image = [] # 20 Product_Image
  100. image_vendor = [] # 21 Vendor_Image
  101. href = [] # 22 Product_Links
  102. cat = soup.find('div', {'class': 'path'}).findAll('a')
  103. listing = soup.find('div', {"class": 'items'}).findAll('form', {"action": '/cart'})
  104. # Populating the Number of Products
  105. nm = len(listing)
  106. for a in listing:
  107. category.append(cat[1].text)
  108. # Adding the url to the list of urls
  109. link = a.find('a').get('href')
  110. link = cleanLink(link)
  111. href.append(link)
  112. # Finding the Product name
  113. product = a.find('a').text
  114. product = product.replace('\n', ' ')
  115. product = product.replace(",", "")
  116. product = product.replace("...", "")
  117. product = product.strip()
  118. name.append(product)
  119. # Finding Product Image
  120. product_image = a.find('img')
  121. if product_image is not None:
  122. product_image = product_image.get('src')
  123. product_image = product_image.split('base64,')[-1]
  124. image.append(product_image)
  125. else:
  126. image.append('-1')
  127. # vendor name
  128. ven = a.find('div', {'class', 'vendor'}).find('a').text
  129. ven = cleanString(ven)
  130. vendor.append(ven)
  131. # find price in usd
  132. usd = a.find('div', {'class': 'price'}).text
  133. usd = usd.replace('$','')
  134. USD.append(usd)
  135. rating_vendor.append('-1')
  136. success.append('-1')
  137. CVE.append('-1')
  138. MS.append('-1')
  139. describe.append('-1')
  140. views.append('-1')
  141. reviews.append('-1')
  142. rating_item.append('-1')
  143. addDate.append('-1')
  144. BTC.append('-1')
  145. EURO.append('-1')
  146. sold.append('-1')
  147. qLeft.append('-1')
  148. shipFrom.append('-1')
  149. shipTo.append('-1')
  150. image_vendor.append('-1')
  151. # Populate the final variable (this should be a list with all fields scraped)
  152. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  153. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href, image, image_vendor)
  154. # called by the crawler to get description links on a listing page
  155. # @param: beautifulsoup object that is using the correct html page (listing page)
  156. # return: list of description links from a listing page
  157. def darkbay_links_parser(soup):
  158. # Returning all links that should be visited by the Crawler
  159. href = []
  160. listing = soup.find('div', {"class": "items"}).findAll('form', {'class':'item'})
  161. for a in listing:
  162. bae = a.findAll('a', href=True)
  163. # Adding the url to the list of urls
  164. link = bae[0].get('href')
  165. href.append(link)
  166. return href