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.

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