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.

234 lines
9.3 KiB

  1. __author__ = 'Helium'
  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 lionmarketplace_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. # vendor name
  34. temp = soup.find('div', {'class': 'btn-group'}).find('a').text
  35. vendor = (cleanString(temp.strip()))
  36. # table with info
  37. table = soup.find('table')
  38. rows = table.findAll('tr')
  39. # vendor rating
  40. pos = soup.find('span', {"class": "fas fa-plus-circle text-success"}).parent.text
  41. pos = int(pos.strip())
  42. neu = soup.find('span', {"class": "fas fa-stop-circle text-secondary"}).parent.text
  43. neu = int(neu.strip())
  44. neg = soup.find('span', {"class": "fas fa-minus-circle text-danger"}).parent.text
  45. neg = int(neg.strip())
  46. total = pos + neu + neg
  47. if total > 0:
  48. rating_vendor = str((pos + 0.5*neu) / total)
  49. # product name
  50. temp = soup.find('div', {'class', 'row'}).find('h2').text
  51. name = (cleanString(temp.strip()))
  52. # product description
  53. temp = soup.find('div', {'class': "mt-4"}).contents[-1]
  54. describe = cleanString(temp.strip())
  55. # Finding Product Image
  56. image = soup.find('div', {'id': 'slide-1'}).find('img')
  57. image = image.get('src')
  58. image = image.split('base64,')[-1]
  59. full = rows[0].findAll('i', {"class": "fas fa-star"})
  60. half = rows[0].find('i', {"class": "fas fa-star-half-alt"})
  61. rating_item = len(full)
  62. if half is not None:
  63. rating_item += 0.5
  64. rating_item = str(rating_item)
  65. # USD selling price
  66. temp = rows[2].find('strong').text
  67. if " $" in temp:
  68. temp = temp.replace(" $", "")
  69. elif "$" in temp:
  70. temp = temp.replace("$", "")
  71. USD = cleanString((temp.strip()))
  72. # product sold
  73. temp = rows[4].find('td')
  74. if temp is not None and cleanString(temp.text.strip()) == 'Left/Sold':
  75. temp = rows[4].findAll('td')
  76. temp = temp[1].findAll('span')
  77. # left
  78. sold = temp[1].text
  79. left = temp[0].text
  80. sold = cleanNumbers(sold.strip())
  81. left = cleanNumbers(left.strip())
  82. else:
  83. sold = '-1'
  84. left = "-1"
  85. # Populating the final variable (this should be a list with all fields scraped)
  86. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  87. BTC, USD, EURO, sold, left, shipFrom, shipTo, image, vendor_image)
  88. # Sending the results
  89. return row
  90. #parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  91. #stores info it needs in different lists, these lists are returned after being organized
  92. #@param: soup object looking at html page of listing page
  93. #return: 'row' that contains a variety of lists that each hold info on the listing page
  94. def lionmarketplace_listing_parser(soup):
  95. # Fields to be parsed
  96. nm = 0 # *Total_Products (Should be Integer)
  97. mktName = "LionMarketplace" # 0 *Marketplace_Name
  98. vendor = [] # 1 *Vendor y
  99. rating_vendor = [] # 2 Vendor_Rating
  100. success = [] # 3 Vendor_Successful_Transactions
  101. name = [] # 4 *Product_Name y
  102. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  103. MS = [] # 6 Product_MS_Classification (Microsoft Security)
  104. category = [] # 7 Product_Category y
  105. describe = [] # 8 Product_Description
  106. views = [] # 9 Product_Number_Of_Views
  107. reviews = [] # 10 Product_Number_Of_Reviews
  108. rating_item = [] # 11 Product_Rating
  109. addDate = [] # 12 Product_AddDate
  110. BTC = [] # 13 Product_BTC_SellingPrice
  111. USD = [] # 14 Product_USD_SellingPrice y
  112. EURO = [] # 15 Product_EURO_SellingPrice
  113. sold = [] # 16 Product_QuantitySold
  114. qLeft =[] # 17 Product_QuantityLeft
  115. shipFrom = [] # 18 Product_ShippedFrom
  116. shipTo = [] # 19 Product_ShippedTo
  117. image = [] # 20 Product_Image
  118. image_vendor = [] # 21 Vendor_Image
  119. href = [] # 22 Product_Links
  120. listings = soup.findAll('div', {"class": "col-md-4 my-md-0 my-2 col-12"})
  121. # Populating the Number of Products
  122. nm = len(listings)
  123. for listing in listings:
  124. a = listing.find('div', {"class": "card-body"})
  125. row = a.findAll('p')
  126. # vendor
  127. temp = row[3].text
  128. temp = temp.replace("Vendor:", "")
  129. vendor.append(cleanString(temp.strip()))
  130. image_vendor.append("-1")
  131. # vendor rating
  132. rating_vendor.append("-1")
  133. # successful transactions CHECK AGAIN HERE
  134. success.append("-1")
  135. # product name
  136. temp = a.find('a').text
  137. name.append(cleanString(temp.strip()))
  138. # Finding Product Image
  139. product_image = listing.find('img', {'class': 'card-img-top rounded'})
  140. product_image = product_image.get('src')
  141. product_image = product_image.split('base64,')[-1]
  142. image.append(product_image)
  143. CVE.append('-1')
  144. MS.append('-1')
  145. # product category
  146. temp = row[1].text
  147. temp = temp.replace("Category: ", "")
  148. category.append(cleanString(temp.strip()))
  149. describe.append('-1')
  150. # product views
  151. vnum = listing.find('p', {"class": "position-absolute bg-primary opacity-60 text-white mt-4 mr-5 pr-1"}).text
  152. views.append(cleanNumbers(vnum.strip()))
  153. reviews.append('-1') # 10 Product_Number_Of_Reviews
  154. rating_item.append('-1') # 11 Product_Rating
  155. addDate.append('-1') # 12 Product_AddDate
  156. # BTC
  157. BTC.append('-1')
  158. # USD
  159. temp = row[0].find('strong').text
  160. USD.append(cleanNumbers(temp.strip())) # 14 Product_USD_SellingPrice
  161. EURO.append("-1") # 15 Product_EURO_SellingPrice
  162. # product sold
  163. sold.append("-1")
  164. qLeft.append('-1') # 17 Product_QuantityLeft
  165. shipFrom.append('-1') # 18 Product_ShippedFrom
  166. shipTo.append('-1') # 19 Product_ShippedTo
  167. # href
  168. temp = a.find('a').get('href')
  169. href.append(temp)
  170. # Populate the final variable (this should be a list with all fields scraped)
  171. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  172. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href, image, image_vendor)
  173. #called by the crawler to get description links on a listing page
  174. #@param: beautifulsoup object that is using the correct html page (listing page)
  175. #return: list of description links from a listing page
  176. def lionmarketplace_links_parser(soup):
  177. # Returning all links that should be visited by the Crawler
  178. href = []
  179. listings = soup.findAll('div', {"class": "col-md-4 my-md-0 my-2 col-12"})
  180. for listing in listings:
  181. a = listing.find('div', {"class": "card-body"})
  182. bae = a.find('a', href=True)
  183. link = bae['href']
  184. href.append(link)
  185. return href