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.

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