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.

195 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. import re
  7. #parses description pages, so takes html pages of description pages using soup object, and parses it for info it needs
  8. #stores info it needs in different lists, these lists are returned after being organized
  9. #@param: soup object looking at html page of description page
  10. #return: 'row' that contains a variety of lists that each hold info on the description page
  11. def AnonMarket_description_parser(soup):
  12. # Fields to be parsed
  13. vendor = "-1" # 0 *Vendor_Name
  14. success = "-1" # 1 Vendor_Successful_Transactions
  15. rating_vendor = "-1" # 2 Vendor_Rating
  16. name = "-1" # 3 *Product_Name
  17. describe = "-1" # 4 Product_Description
  18. CVE = "-1" # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  19. MS = "-1" # 6 Product_MS_Classification (Microsoft Security)
  20. category = "-1" # 7 Product_Category
  21. views = "-1" # 8 Product_Number_Of_Views
  22. reviews = "-1" # 9 Product_Number_Of_Reviews
  23. rating_item = "-1" # 10 Product_Rating
  24. addDate = "-1" # 11 Product_AddedDate
  25. BTC = "-1" # 12 Product_BTC_SellingPrice
  26. USD = "-1" # 13 Product_USD_SellingPrice
  27. EURO = "-1" # 14 Product_EURO_SellingPrice
  28. sold = "-1" # 15 Product_QuantitySold
  29. left = "-1" # 16 Product_QuantityLeft
  30. shipFrom = "-1" # 17 Product_ShippedFrom
  31. shipTo = "-1" # 18 Product_ShippedTo
  32. image = "-1" # 19 Product_Image
  33. vendor_image = "-1" # 20 Vendor_Image
  34. name_of_product = soup.find("div", {"class": "heading"}).text
  35. name = cleanString(name_of_product.strip())
  36. description_div = soup.find("div", {"class": "tab1"})
  37. if description_div is None:
  38. describe = "-1"
  39. else:
  40. describe = cleanString(description_div.text.strip())
  41. info_div = soup.find('div', {'class': 'information'})
  42. table = info_div.find('table') if info_div else None
  43. # Find all table rows
  44. rows = table.find_all('tr')
  45. # Parse each row to get relevant data
  46. data = {}
  47. for row in rows:
  48. columns = row.find_all('td')
  49. if len(columns) == 3:
  50. key = columns[0].text.strip()
  51. value = columns[2].text.strip()
  52. data[key] = value
  53. # Extract specific data from the dictionary and assign them to individual variables
  54. vendor = data.get('Vendor', '-1')
  55. shipFrom = data.get('Location', '-1')
  56. shipTo = data.get('Ships to', '-1')
  57. category = data.get('Category', '-1')
  58. USD = data.get('Price', '-1').split()[0]
  59. left = data.get('Stock', '-1')
  60. # image
  61. image = soup.find('img', {"class": "bigthumbnail"})
  62. image = image.get('src').split('base64,')[-1]
  63. # Populating the final variable (this should be a list with all fields scraped)
  64. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  65. BTC, USD, EURO, sold, left, shipFrom, shipTo, image, vendor_image)
  66. # Sending the results
  67. return row
  68. #parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  69. #stores info it needs in different lists, these lists are returned after being organized
  70. #@param: soup object looking at html page of listing page
  71. #return: 'row' that contains a variety of lists that each hold info on the listing page
  72. def AnonMarket_listing_parser(soup):
  73. # Fields to be parsed
  74. nm = 0 # *Total_Products (Should be Integer)
  75. mktName = "AnonMarket" # 0 *Marketplace_Name
  76. vendor = [] # 1 *Vendor y
  77. rating_vendor = [] # 2 Vendor_Rating
  78. success = [] # 3 Vendor_Successful_Transactions
  79. name = [] # 4 *Product_Name y
  80. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about this
  81. MS = [] # 6 Product_MS_Classification (Microsoft Security) dont worry about this
  82. category = [] # 7 Product_Category y
  83. describe = [] # 8 Product_Description
  84. views = [] # 9 Product_Number_Of_Views
  85. reviews = [] # 10 Product_Number_Of_Reviews
  86. rating_item = [] # 11 Product_Rating
  87. addDate = [] # 12 Product_AddDate
  88. BTC = [] # 13 Product_BTC_SellingPrice
  89. USD = [] # 14 Product_USD_SellingPrice y
  90. EURO = [] # 15 Product_EURO_SellingPrice
  91. sold = [] # 16 Product_QuantitySold
  92. qLeft = [] # 17 Product_QuantityLeft
  93. shipFrom = [] # 18 Product_ShippedFrom
  94. shipTo = [] # 19 Product_ShippedTo
  95. image = [] # 20 Product_Image
  96. image_vendor = [] # 21 Vendor_Image
  97. href = [] # 22 Product_Links
  98. base_url = "http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion"
  99. cat = soup.find("div", {'class': 'heading'}).text
  100. products_list = soup.find_all('div', {'class': 'item'})
  101. nm = 0
  102. for product in products_list:
  103. name_of_product = product.find("div", {"class": "title"}).text.strip()
  104. name.append(name_of_product)
  105. name_of_vendor = product.find("a", {'class': 'seller'}).text.strip()
  106. vendor.append(name_of_vendor)
  107. category.append(cat)
  108. tbody = product.find('div', {"class": "info"}).find('tbody')
  109. # rating_item
  110. width = tbody.find('div', {"class": "stars2"}).get('style')
  111. rating_item.append(cleanNumbers(width.strip()))
  112. tr = tbody.findAll('tr', recursive=False)
  113. td = tr[2].findAll('td')
  114. # sold
  115. sold.append(td[0].text.strip())
  116. # reviews
  117. reviews.append(td[1].text.strip())
  118. product_link_element = product.find("div", {"class": "title"}).find_parent('a')
  119. link = product_link_element['href']
  120. full_link = base_url + link
  121. href.append(full_link)
  122. # Append '-1' for unavailable data
  123. rating_vendor.append("-1")
  124. success.append("-1")
  125. CVE.append("-1")
  126. MS.append("-1")
  127. describe.append("-1")
  128. views.append("-1")
  129. addDate.append("-1")
  130. BTC.append("-1")
  131. USD.append("-1")
  132. EURO.append("-1")
  133. qLeft.append("-1")
  134. shipFrom.append("-1")
  135. shipTo.append("-1")
  136. nm += 1
  137. # Populate the final variable (this should be a list with all fields scraped)
  138. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  139. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href, image, image_vendor)
  140. #called by the crawler to get description links on a listing page
  141. #@param: beautifulsoup object that is using the correct html page (listing page)
  142. #return: list of description links from a listing page
  143. def AnonMarket_links_parser(soup):
  144. # Base URL to prepend to each product link
  145. base_url = "http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion"
  146. # Returning all links that should be visited by the Crawler
  147. href = []
  148. # Using a shorter, but still unique, class name
  149. listing = soup.find('div', {'class': 'items'}).find_all('a', href=True, attrs={'href': lambda x: "/product/" in x})
  150. for a in listing:
  151. link = a.get('href')
  152. if link: # Checks if 'href' attribute is not None
  153. # Prepending the base URL to the scraped link
  154. full_link = base_url + link
  155. href.append(full_link)
  156. # Filtering out any links that might not have '/product/' in them
  157. product_links = [link for link in href if '/product/' in link]
  158. return product_links