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.

249 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 mgm_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. divmb = soup.find('div', {'class': "col-md-12 my-2"})
  35. name = divmb.find('span', {'class': "btn btn-sm btn-outline-dark w-100 active rounded-0"}).text
  36. name = name.replace('\n', ' ')
  37. name = name.replace(",", "")
  38. name = name.strip()
  39. box = soup.find('div', {'class': "col-md-7"}).find('span')
  40. box = box.findAll('span', {'class': "btn btn-mgray btn-sm w-100 active border-danger"})
  41. # Finding Vendor
  42. vendor = soup.find('a', {'class': "btn btn-sm btn-mgray my-1 w-100 text-white"}).get('href')
  43. vendor = vendor.split('otherParty=')[-1]
  44. vendor = cleanString(vendor).strip()
  45. # Finding Vendor Rating
  46. temp = box[1]
  47. rating_vendor = len(temp.findAll('i', {"class": "fas fa-star"}))
  48. half_stars = len(temp.findAll('i', {'class': "fas fa-star-half-alt"}))
  49. if half_stars > 0:
  50. rating_vendor += 0.5
  51. # Finding Successful Transactions
  52. success = box[2].text
  53. success = cleanNumbers(success).strip()
  54. box2 = soup.find('div', {"class": "col-md-4 text-center"}).find('span', {"class": "text-left"}).findAll('span')
  55. # Finding USD
  56. USD = box2[0].text
  57. USD = USD.replace('\n', '')
  58. USD = USD.replace('$', '')
  59. USD = USD.strip()
  60. # Finding Vendor Image
  61. vendor_image = soup.find('img', {"class": 'img-fluid'}).get('src')
  62. vendor_image = vendor_image.split('base64,')[-1]
  63. # Finding the Product description
  64. temp = soup.find('div', {"class": 'row-md-12'}).find('div', {"class": 'col-md-4'})
  65. cardbody = temp.find('textarea', {"class": 'disabled form-control form-control-sm w-100 bg-mgray text-white rounded-0 border-danger'})
  66. describe = cleanString(cardbody.text).strip()
  67. # Finding Product Image
  68. image = soup.find('div', {"class": 'row-md-12'}).find('div', {"class": 'col-md-4 text-center'}).find('img')
  69. if image is not None:
  70. image = image.get('src')
  71. image = image.split('base64,')[-1]
  72. else:
  73. image = "-1"
  74. # Searching for CVE and MS categories
  75. cve = soup.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  76. if cve:
  77. CVE = " "
  78. for idx in cve:
  79. CVE += (idx)
  80. CVE += " "
  81. CVE = CVE.replace(',', ' ')
  82. CVE = CVE.replace('\n', '')
  83. ms = soup.findAll(text=re.compile('MS\d{2}-\d{3}'))
  84. if ms:
  85. MS = " "
  86. for im in ms:
  87. MS += (im)
  88. MS += " "
  89. MS = MS.replace(',', ' ')
  90. MS = MS.replace('\n', '')
  91. # Populating the final variable (this should be a list with all fields scraped)
  92. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  93. BTC, USD, EURO, sold, left, shipFrom, shipTo, image, vendor_image)
  94. # Sending the results
  95. return row
  96. # parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  97. # stores info it needs in different lists, these lists are returned after being organized
  98. # @param: soup object looking at html page of listing page
  99. # return: 'row' that contains a variety of lists that each hold info on the listing page
  100. def mgm_listing_parser(soup):
  101. # Fields to be parsed
  102. nm = 0 # *Total_Products (Should be Integer)
  103. mktName = "Ares" # 0 *Marketplace_Name
  104. vendor = [] # 1 *Vendor y
  105. rating_vendor = [] # 2 Vendor_Rating
  106. success = [] # 3 Vendor_Successful_Transactions
  107. name = [] # 4 *Product_Name y
  108. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about this
  109. MS = [] # 6 Product_MS_Classification (Microsoft Security) dont worry about this
  110. category = [] # 7 Product_Category y
  111. describe = [] # 8 Product_Description
  112. views = [] # 9 Product_Number_Of_Views
  113. reviews = [] # 10 Product_Number_Of_Reviews
  114. rating_item = [] # 11 Product_Rating
  115. addDate = [] # 12 Product_AddDate
  116. BTC = [] # 13 Product_BTC_SellingPrice
  117. USD = [] # 14 Product_USD_SellingPrice y
  118. EURO = [] # 15 Product_EURO_SellingPrice
  119. sold = [] # 16 Product_QuantitySold
  120. qLeft = [] # 17 Product_QuantityLeft
  121. shipFrom = [] # 18 Product_ShippedFrom
  122. shipTo = [] # 19 Product_ShippedTo
  123. image = [] # 20 Product_Image
  124. image_vendor = [] # 21 Vendor_Image
  125. href = [] # 22 Product_Links
  126. cat = soup.find('span', {"class": "btn btn-sm btn-outline-dark w-100 active"}).text
  127. cat = cleanString(cat).strip()
  128. listing = soup.find('div', {"class": 'card-body text-black text-left bg-dark'}).findAll('div', {"class": 'card mb-4 border-danger rounded-0'})
  129. # Populating the Number of Products
  130. nm = len(listing)
  131. for a in listing:
  132. category.append(cat)
  133. # Adding the url to the list of urls
  134. link = a.find('a', {'class': "badge badge-danger w-100 text-white"}).get('href')
  135. link = cleanLink(link)
  136. href.append(link)
  137. # Finding the Product name
  138. product = a.find('div', {"class": 'marquee-parent'}).find('div', {"class": "marquee-child"}).text
  139. product = product.replace('\n', ' ')
  140. product = product.replace(",", "")
  141. product = product.replace("...", "")
  142. product = product.strip()
  143. name.append(product)
  144. # Finding Product Image
  145. product_image = a.find('img')
  146. product_image = product_image.get('src')
  147. product_image = product_image.split('base64,')[-1]
  148. image.append(product_image)
  149. # Finding Prices
  150. price = a.findAll('a', {"class": "text-white"})[-1].text
  151. price = price.replace("$","")
  152. price = price.strip()
  153. USD.append(price)
  154. # Finding Item Rating
  155. temp = a.find('small', {"class": "text-white"})
  156. rating = len(temp.findAll('i', {"class": "fas fa-star"}))
  157. half_stars = len(temp.findAll('i', {'class': "fas fa-star-half-alt"}))
  158. if half_stars > 0:
  159. rating += 0.5
  160. rating_item.append(str(rating))
  161. # Finding the Vendor
  162. vendor_name = a.find('a', {"class": 'badge badge-dark w-100 text-white my-1'}).text
  163. vendor_name = vendor_name.replace(",", "")
  164. vendor_name = vendor_name.strip()
  165. vendor.append(vendor_name)
  166. image_vendor.append("-1")
  167. # Searching for CVE and MS categories
  168. cve = a.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  169. if not cve:
  170. cveValue = "-1"
  171. else:
  172. cee = " "
  173. for idx in cve:
  174. cee += (idx)
  175. cee += " "
  176. cee = cee.replace(',', ' ')
  177. cee = cee.replace('\n', '')
  178. cveValue = cee
  179. CVE.append(cveValue)
  180. ms = a.findAll(text=re.compile('MS\d{2}-\d{3}'))
  181. if not ms:
  182. MSValue = "-1"
  183. else:
  184. me = " "
  185. for im in ms:
  186. me += (im)
  187. me += " "
  188. me = me.replace(',', ' ')
  189. me = me.replace('\n', '')
  190. MSValue = me
  191. MS.append(MSValue)
  192. # Populate the final variable (this should be a list with all fields scraped)
  193. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  194. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href, image, image_vendor)
  195. # called by the crawler to get description links on a listing page
  196. # @param: beautifulsoup object that is using the correct html page (listing page)
  197. # return: list of description links from a listing page
  198. def mgm_links_parser(soup):
  199. # Returning all links that should be visited by the Crawler
  200. href = []
  201. listing = soup.find('div', {"class": "list-products columns-3"}).find_all('div', {'class':'product-item hover-shadow'})
  202. for a in listing:
  203. bae = a.findAll('a', href=True)
  204. # Adding the url to the list of urls
  205. link = bae[0].get('href')
  206. href.append(link)
  207. return href