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.

236 lines
9.7 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. usd_to_brl_r = None
  8. #parses description pages, so takes html pages of description pages using soup object, and parses it for info it needs
  9. #stores info it needs in different lists, these lists are returned after being organized
  10. #@param: soup object looking at html page of description page
  11. #return: 'row' that contains a variety of lists that each hold info on the description page
  12. def nexus_description_parser(soup):
  13. # Fields to be parsed
  14. vendor = "-1" # 0 *Vendor_Name
  15. success = "-1" # 1 Vendor_Successful_Transactions
  16. rating_vendor = "-1" # 2 Vendor_Rating
  17. name = "-1" # 3 *Product_Name
  18. describe = "-1" # 4 Product_Description
  19. CVE = "-1" # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  20. MS = "-1" # 6 Product_MS_Classification (Microsoft Security)
  21. category = "-1" # 7 Product_Category
  22. views = "-1" # 8 Product_Number_Of_Views
  23. reviews = "-1" # 9 Product_Number_Of_Reviews
  24. rating_item = "-1" # 10 Product_Rating
  25. addDate = "-1" # 11 Product_AddedDate
  26. BTC = "-1" # 12 Product_BTC_SellingPrice
  27. USD = "-1" # 13 Product_USD_SellingPrice
  28. EURO = "-1" # 14 Product_EURO_SellingPrice
  29. sold = "-1" # 15 Product_QuantitySold
  30. left = "-1" # 16 Product_QuantityLeft
  31. shipFrom = "-1" # 17 Product_ShippedFrom
  32. shipTo = "-1" # 18 Product_ShippedTo
  33. image = "-1" # 19 Product_Image
  34. vendor_image = "-1" # 20 Vendor_Image
  35. #finding the name of the product
  36. name_of_product = soup.find("h1", {"class": "product_title entry-title"}).text
  37. name = cleanString(name_of_product.strip())
  38. # Finding USD Price
  39. real = soup.find('span', {"class": "price"}).find('bdi').text
  40. real = real.split(',')
  41. whole = cleanNumbers(real[0]).replace('.', '')
  42. real = whole + '.' + real[1]
  43. usd = float(real) / usd_to_brl_r
  44. USD = str(round(usd, 2))
  45. # Find the BTC Price
  46. prices = soup.find('p', {"class": "price"}).findAll('span', {"class": "cs"})
  47. if len(prices) > 0:
  48. BTC = prices[0].text
  49. BTC = cleanNumbers(BTC.strip())
  50. # finding the description of the product
  51. description_div = soup.find("div", {"class": "woocommerce-product-details__short-description"})
  52. if description_div is None:
  53. describe = "-1"
  54. else:
  55. describe = cleanString(description_div.text.strip())
  56. # Finding Product Image
  57. image = soup.find('div', {'class': 'woocommerce-product-gallery__wrapper'}).find('img')
  58. image = image.get('src')
  59. image = image.split('base64,')[-1]
  60. #find the category of the product
  61. name_of_category = soup.find("span", {"class": "posted_in"}).find("a").text
  62. category = cleanString(name_of_category.strip())
  63. #finding the name of the vendor
  64. name_of_vendor = soup.find("div", {"class": "dokan-vendor-name"}).find("h5").text
  65. vendor = cleanString(name_of_vendor)
  66. #finding the vendor's rating
  67. vendorRating = soup.find("div", {"class": "dokan-vendor-rating"}).find("p").text
  68. rating_vendor = cleanString(vendorRating)
  69. #everything else gets a -1 because they are not found
  70. # Populating the final variable (this should be a list with all fields scraped)
  71. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  72. BTC, USD, EURO, sold, left, shipFrom, shipTo, image, vendor_image)
  73. # Sending the results
  74. return row
  75. #parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  76. #stores info it needs in different lists, these lists are returned after being organized
  77. #@param: soup object looking at html page of listing page
  78. #return: 'row' that contains a variety of lists that each hold info on the listing page
  79. def nexus_listing_parser(soup):
  80. global usd_to_brl_r
  81. while usd_to_brl_r is None:
  82. try:
  83. usd_to_brl_r = float(input("1 US Dollar = (Brazilian Real) "))
  84. except ValueError:
  85. pass
  86. # Fields to be parsed
  87. nm = 0 # *Total_Products (Should be Integer)
  88. mktName = "Nexus" # 0 *Marketplace_Name
  89. vendor = [] # 1 *Vendor y
  90. rating_vendor = [] # 2 Vendor_Rating
  91. success = [] # 3 Vendor_Successful_Transactions
  92. name = [] # 4 *Product_Name y
  93. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  94. MS = [] # 6 Product_MS_Classification (Microsoft Security)
  95. category = [] # 7 Product_Category y
  96. describe = [] # 8 Product_Description
  97. views = [] # 9 Product_Number_Of_Views
  98. reviews = [] # 10 Product_Number_Of_Reviews
  99. rating_item = [] # 11 Product_Rating
  100. addDate = [] # 12 Product_AddDate
  101. BTC = [] # 13 Product_BTC_SellingPrice
  102. USD = [] # 14 Product_USD_SellingPrice y
  103. EURO = [] # 15 Product_EURO_SellingPrice
  104. sold = [] # 16 Product_QuantitySold
  105. qLeft =[] # 17 Product_QuantityLeft
  106. shipFrom = [] # 18 Product_ShippedFrom
  107. shipTo = [] # 19 Product_ShippedTo
  108. image = [] # 20 Product_Image
  109. image_vendor = [] # 21 Vendor_Image
  110. href = [] # 22 Product_Links
  111. main = soup.find('main', {'id': 'main'})
  112. products_list = main.find('ul', recursive=False).find_all('li', recursive=False)
  113. nm = len(products_list)
  114. for product in products_list:
  115. # Finding the name of the product
  116. name_of_product = product.find("h2", {"class": "woocommerce-loop-product__title"}).find("a").text
  117. name_of_product_cleaned = cleanString(name_of_product.strip())
  118. # print(name_of_product_cleaned)
  119. name.append(name_of_product_cleaned)
  120. #finding the URL
  121. try:
  122. url = product.find("a", class_="woocommerce-loop-product__link").get('href')
  123. href.append(url)
  124. except AttributeError as e:
  125. print("I can't find the link")
  126. raise e
  127. # Finding Product Image
  128. product_image = product.find('a', {'class': 'woocommerce-loop-image-link woocommerce-LoopProduct-link woocommerce-loop-product__link'}).find('img')
  129. product_image = product_image.get('src')
  130. product_image = product_image.split('base64,')[-1]
  131. image.append(product_image)
  132. # Finding USD Price
  133. real = product.find('span', {"class": "price"}).find('bdi').text
  134. real = real.split(',')
  135. whole = cleanNumbers(real[0]).replace('.', '')
  136. real = whole + '.' + real[1]
  137. usd = float(real) / usd_to_brl_r
  138. USD.append(str(round(usd, 2)))
  139. # Finding BTC Price
  140. prices = product.find('span', {"class": "price"}).findAll('span', {"class": "cs"})
  141. if len(prices) > 0:
  142. price = prices[0].text
  143. BTC.append(cleanNumbers(price.strip()))
  144. #everything else appends a -1
  145. rating_vendor.append("-1")
  146. vendor.append('-1')
  147. success.append("-1")
  148. CVE.append("-1")
  149. MS.append("-1")
  150. category.append("-1")
  151. describe.append("-1")
  152. views.append("-1")
  153. reviews.append("-1")
  154. addDate.append("-1")
  155. EURO.append("-1")
  156. sold.append("-1")
  157. qLeft.append("-1")
  158. shipFrom.append("-1")
  159. shipTo.append("-1")
  160. image_vendor.append("-1")
  161. # Populate the final variable (this should be a list with all fields scraped)
  162. return organizeProducts(
  163. marketplace = mktName,
  164. nm = nm,
  165. vendor = vendor,
  166. rating_vendor = rating_vendor,
  167. success_vendor = success,
  168. nombre = name,
  169. CVE = CVE,
  170. MS = MS,
  171. category = category,
  172. describe = describe,
  173. views = views,
  174. reviews = reviews,
  175. rating_item = rating_item,
  176. addDate = addDate,
  177. BTC = BTC,
  178. USD = USD,
  179. EURO = EURO,
  180. sold = sold,
  181. qLeft = qLeft,
  182. shipFrom = shipFrom,
  183. shipTo = shipTo,
  184. href = href,
  185. image = image,
  186. image_vendor = image_vendor
  187. )
  188. #called by the crawler to get description links on a listing page
  189. #@param: beautifulsoup object that is using the correct html page (listing page)
  190. #return: list of description links from a listing page
  191. def nexus_links_parser(soup):
  192. # Returning all links that should be visited by the Crawler
  193. href = []
  194. # Using a shorter, but still unique, class name
  195. listing = soup.find_all("a", class_="woocommerce-loop-product__link")
  196. for a in listing:
  197. link = a.get('href')
  198. if link: # Checks if 'href' attribute is not None
  199. href.append(link)
  200. return href