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.

252 lines
9.6 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. # successful transaction
  40. success = "-1"
  41. # vendor rating 5
  42. rating_vendor = '-1'
  43. # product name
  44. temp = soup.find('div', {'class', 'row'}).find('h2').text
  45. name = (cleanString(temp.strip()))
  46. # product description
  47. temp = soup.find('div', {'class': "mt-4"}).find(text=True, recursive=False)
  48. describe = cleanString(temp.strip())
  49. # Finding Product Image
  50. image = soup.find('div', {'id': 'slide-1'}).find('img')
  51. image = image.get('src')
  52. image = image.split('base64,')[-1]
  53. CVE = "-1" # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about that much
  54. MS = "-1" # 6 Product_MS_Classification (Microsoft Security) dont worry about that much
  55. # product category
  56. temp = rows[1].find('strong').text
  57. category = cleanString(temp.strip())
  58. # product number of views
  59. views = "-1"
  60. reviews = "-1" # 9 Product_Number_Of_Reviews
  61. rating_item = "-1" # 10 Product_Rating
  62. addDate = "-1" # 11 Product_AddedDate
  63. # BTC selling price box box-rounded mt-2
  64. BTC = "-1"
  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. EURO = "-1" # 14 Product_EURO_SellingPrice
  73. # product sold
  74. if (len(rows) <= 5):
  75. temp = rows[4].find('td').text
  76. string = cleanString(temp)
  77. if (string == 'Left/Sold'):
  78. temp = rows[4].findAll('td')
  79. temp = temp[1].findAll('span')
  80. # left
  81. temp2 = temp[1].text
  82. temp3 = temp[1].text
  83. if(" items" in temp2):
  84. temp2 = temp2.replace(" items", "")
  85. if(" items" in temp3):
  86. temp3 = temp3.replace(" items", "")
  87. sold = (cleanString(temp2.strip()))
  88. left = cleanString(temp3.strip())
  89. else:
  90. sold = '-1'
  91. left = "-1"
  92. else:
  93. sold = '-1'
  94. left = "-1"
  95. shipFrom = "-1" # 17 Product_ShippedFrom
  96. shipTo = "-1" # 18 Product_ShippedTo
  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 lionmarketplace_listing_parser(soup):
  107. # Fields to be parsed
  108. nm = 0 # *Total_Products (Should be Integer)
  109. mktName = "LionMarketplace" # 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)
  115. MS = [] # 6 Product_MS_Classification (Microsoft Security)
  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. listings = soup.findAll('div', {"class": "col-md-4 my-md-0 my-2 col-12"})
  133. # Populating the Number of Products
  134. nm = len(listings)
  135. for listing in listings:
  136. a = listing.find('div', {"class": "card-body"})
  137. row = a.findAll('p')
  138. # vendor
  139. temp = row[3].text
  140. temp = temp.replace("Vendor:", "")
  141. vendor.append(cleanString(temp.strip()))
  142. image_vendor.append("-1")
  143. # vendor rating
  144. rating_vendor.append("-1")
  145. # successful transactions CHECK AGAIN HERE
  146. success.append("-1")
  147. # product name
  148. temp = a.find('a').text
  149. name.append(cleanString(temp.strip()))
  150. # Finding Product Image
  151. product_image = listing.find('img', {'class': 'card-img-top rounded'})
  152. product_image = product_image.get('src')
  153. product_image = product_image.split('base64,')[-1]
  154. image.append(product_image)
  155. CVE.append('-1')
  156. MS.append('-1')
  157. # product category
  158. temp = row[2].text
  159. temp = temp.replace("Category: ", "")
  160. category.append(cleanString(temp.strip()))
  161. describe.append('-1')
  162. # product views
  163. views.append("-1")
  164. reviews.append('-1') # 10 Product_Number_Of_Reviews
  165. rating_item.append('-1') # 11 Product_Rating
  166. addDate.append('-1') # 12 Product_AddDate
  167. # BTC
  168. BTC.append('-1')
  169. # USD
  170. temp = row[0].find('strong').text
  171. if ' $' in temp:
  172. temp = temp.replace(" $", "")
  173. USD.append(cleanString(temp.strip())) # 14 Product_USD_SellingPrice
  174. EURO.append("-1") # 15 Product_EURO_SellingPrice
  175. # product sold
  176. sold.append("-1")
  177. qLeft.append('-1') # 17 Product_QuantityLeft
  178. shipFrom.append('-1') # 18 Product_ShippedFrom
  179. shipTo.append('-1') # 19 Product_ShippedTo
  180. # href
  181. temp = a.find('a').get('href')
  182. href.append(temp)
  183. # Populate the final variable (this should be a list with all fields scraped)
  184. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  185. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href, image, image_vendor)
  186. #called by the crawler to get description links on a listing page
  187. #@param: beautifulsoup object that is using the correct html page (listing page)
  188. #return: list of description links from a listing page
  189. def lionmarketplace_links_parser(soup):
  190. # Returning all links that should be visited by the Crawler
  191. href = []
  192. listings = soup.findAll('div', {"class": "col-md-4 my-md-0 my-2 col-12"})
  193. for listing in listings:
  194. a = listing.find('div', {"class": "card-body"})
  195. bae = a.find('a', href=True)
  196. link = bae['href']
  197. href.append(link)
  198. return href