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