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.

259 lines
11 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 MikesGrandStore_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. # Finding Product Name
  34. name = soup.find('h1', {'class': 'product-title product_title entry-title'}).text
  35. name = name.replace('\n', ' ')
  36. name = name.replace(",", "")
  37. name = name.strip()
  38. divmb = soup.findAll('div', {'class': "mb-1"})
  39. # Finding Vendor
  40. # no vendor
  41. vendor = "MikesGrandStore"
  42. # Finding the Product Rating
  43. rating_item = soup.find('strong', {'class', 'rating'}).text
  44. rating_item = rating_item.replace('\n', ' ')
  45. rating_item = rating_item.replace(",", "")
  46. rating_item = rating_item.strip()
  47. # Finding Number of Product Reviews
  48. review_container = soup.find('li', {'id': 'tab-title-reviews'})
  49. reviews = review_container.find('a').text
  50. reviews = reviews.replace('Reviews', '')
  51. reviews = reviews.replace('(', '')
  52. reviews = reviews.replace(')', '')
  53. reviews = reviews.replace('\n', ' ')
  54. reviews = reviews.replace(",", "")
  55. reviews = reviews.strip()
  56. # Finding Prices
  57. USD = soup.find('span', {'class': 'woocommerce-Price-currencySymbol'}).next_sibling
  58. USD = USD.replace('\n', ' ')
  59. USD = USD.replace(",", "")
  60. USD = USD.strip()
  61. # Finding the Product Category
  62. cat_container = soup.find('span', {'class': 'posted_in'})
  63. cat = cat_container.findAll('a')
  64. category = ""
  65. for name in cat:
  66. category = category + " " + name.text
  67. # Finding the Product Quantity Available
  68. stock = soup.find('p', {'class': 'stock in-stock'})
  69. if stock is not None:
  70. left = stock.text
  71. left = left.replace("in stock", "")
  72. left = left.strip()
  73. # Finding the Product description
  74. desc_cont = soup.find('div', {'class': 'product-short-description'})
  75. describe = desc_cont.find('p').text.strip()
  76. # Finding Product Image
  77. image = soup.find('img', {'class': 'wp-post-image skip-lazy'})
  78. image = image.get('src')
  79. image = image.split('base64,')[-1]
  80. # Searching for CVE and MS categories
  81. cve = soup.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  82. if cve:
  83. CVE = " "
  84. for idx in cve:
  85. CVE += (idx)
  86. CVE += " "
  87. CVE = CVE.replace(',', ' ')
  88. CVE = CVE.replace('\n', '')
  89. ms = soup.findAll(text=re.compile('MS\d{2}-\d{3}'))
  90. if ms:
  91. MS = " "
  92. for im in ms:
  93. MS += (im)
  94. MS += " "
  95. MS = MS.replace(',', ' ')
  96. MS = MS.replace('\n', '')
  97. # Populating the final variable (this should be a list with all fields scraped)
  98. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  99. BTC, USD, EURO, sold, left, shipFrom, shipTo, image, vendor_image)
  100. # Sending the results
  101. return row
  102. # parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  103. # stores info it needs in different lists, these lists are returned after being organized
  104. # @param: soup object looking at html page of listing page
  105. # return: 'row' that contains a variety of lists that each hold info on the listing page
  106. def MikesGrandStore_listing_parser(soup):
  107. # Fields to be parsed
  108. nm = 0 # *Total_Products (Should be Integer)
  109. mktName = "MikesGrandStore" # 0 *Marketplace_Name
  110. vendor = [] # 1 *Vendor y
  111. rating_vendor = [] # 2 Vendor_Rating
  112. success = [] # 3 Vendor_Successful_Transactions
  113. name = [] # 4 *Product_Name y
  114. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about this
  115. MS = [] # 6 Product_MS_Classification (Microsoft Security) dont worry about this
  116. category = [] # 7 Product_Category y
  117. describe = [] # 8 Product_Description
  118. views = [] # 9 Product_Number_Of_Views
  119. reviews = [] # 10 Product_Number_Of_Reviews
  120. rating_item = [] # 11 Product_Rating
  121. addDate = [] # 12 Product_AddDate
  122. BTC = [] # 13 Product_BTC_SellingPrice
  123. USD = [] # 14 Product_USD_SellingPrice y
  124. EURO = [] # 15 Product_EURO_SellingPrice
  125. sold = [] # 16 Product_QuantitySold
  126. qLeft = [] # 17 Product_QuantityLeft
  127. shipFrom = [] # 18 Product_ShippedFrom
  128. shipTo = [] # 19 Product_ShippedTo
  129. image = [] # 20 Product_Image
  130. image_vendor = [] # 21 Vendor_Image
  131. href = [] # 22 Product_Links
  132. listing_container = soup.find('div', {'class': 'products row row-small large-columns-3 medium-columns-3 small-columns-2 equalize-box'})
  133. listing = listing_container.findAll('div', recursive=False)
  134. # Populating the Number of Products
  135. nm = len(listing)
  136. for a in listing:
  137. bae = a.findAll('a', href=True)
  138. lb = a.findAll('div', {"id": "littlebox"})
  139. # Adding the url to the list of urls
  140. link = a.find('a', {'class': 'woocommerce-LoopProduct-link woocommerce-loop-product__link'}).get('href')
  141. href.append(link)
  142. # Finding the Product
  143. product = a.find('a', {'class': 'woocommerce-LoopProduct-link woocommerce-loop-product__link'}).text
  144. product = product.replace('\n', ' ')
  145. product = product.replace(",", "")
  146. product = product.replace("...", "")
  147. product = product.strip()
  148. name.append(product)
  149. # Finding Product Image
  150. product_image = a.find('img', {'class': 'attachment-woocommerce_thumbnail size-woocommerce_thumbnail'})
  151. product_image = product_image.get('src')
  152. product_image = product_image.split('base64,')[-1]
  153. image.append(product_image)
  154. # Finding Prices
  155. price = a.find('span', {'class': 'woocommerce-Price-currencySymbol'}).next_sibling
  156. price = price.strip()
  157. USD.append(price)
  158. # Finding the Vendor
  159. vendor_name = "MikesGrandStore"
  160. vendor.append(vendor_name)
  161. image_vendor.append("-1")
  162. # Finding the Category
  163. cat = a.find('p', {'class': 'category uppercase is-smaller no-text-overflow product-cat op-7'}).text
  164. cat = cat.replace("class:", "")
  165. cat = cat.strip()
  166. category.append(cat)
  167. # Finding product rating
  168. rating = a.find('strong', {'class': 'rating'}).text
  169. rating = rating.strip()
  170. rating_item.append(rating)
  171. # Searching for CVE and MS categories
  172. cve = a.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  173. if not cve:
  174. cveValue = "-1"
  175. else:
  176. cee = " "
  177. for idx in cve:
  178. cee += (idx)
  179. cee += " "
  180. cee = cee.replace(',', ' ')
  181. cee = cee.replace('\n', '')
  182. cveValue = cee
  183. CVE.append(cveValue)
  184. ms = a.findAll(text=re.compile('MS\d{2}-\d{3}'))
  185. if not ms:
  186. MSValue = "-1"
  187. else:
  188. me = " "
  189. for im in ms:
  190. me += (im)
  191. me += " "
  192. me = me.replace(',', ' ')
  193. me = me.replace('\n', '')
  194. MSValue = me
  195. MS.append(MSValue)
  196. # Populate the final variable (this should be a list with all fields scraped)
  197. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  198. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href, image, image_vendor)
  199. # called by the crawler to get description links on a listing page
  200. # @param: beautifulsoup object that is using the correct html page (listing page)
  201. # return: list of description links from a listing page
  202. def MikesGrandStore_links_parser(soup):
  203. # Returning all links that should be visited by the Crawler
  204. href = []
  205. container = soup.find('div', {"class": "products row row-small large-columns-3 medium-columns-3 small-columns-2 equalize-box"})
  206. listing = container.findAll('div', recursive=False)
  207. # for a in listing:
  208. # bae = a.find('a', {"class": "text-info"}, href=True)
  209. # link = bae['href']
  210. # href.append(link)
  211. for a in listing:
  212. bae = a.findAll('a', href=True)
  213. # Adding the url to the list of urls
  214. link = bae[0].get('href')
  215. href.append(link)
  216. return href