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.

285 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) dont worry about that much
  18. MS = "-1" # 6 Product_MS_Classification (Microsoft Security) dont worry about that much
  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. # vendor name
  32. try:
  33. temp = soup.find('div', {'class': 'btn-group'}).find('a').text
  34. vendor = (cleanString(temp.strip()))
  35. except:
  36. print('vendor')
  37. vendor = "-1"
  38. # table with info
  39. table = soup.find('table', {'class', 'table border-0 text-left table-borderless'})
  40. rows = table.findAll('tr')
  41. # successful transaction
  42. success = "-1"
  43. # vendor rating 5
  44. rating_vendor = '-1'
  45. # product name
  46. try:
  47. temp = soup.find('div', {'class', 'row'}).find('h2').text
  48. name = (cleanString(temp.strip()))
  49. except:
  50. name = '-1'
  51. print('product name')
  52. # product description
  53. try:
  54. temp = soup.find('div', {'class': "mt-4"}).findAll('p')
  55. temp = temp[1].text
  56. if "\n" in temp:
  57. temp = temp.replace("\n", " ")
  58. temp = temp.replace("\r", " ")
  59. describe = cleanString(temp.strip())
  60. except:
  61. describe="-1"
  62. print('describe')
  63. CVE = "-1" # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about that much
  64. MS = "-1" # 6 Product_MS_Classification (Microsoft Security) dont worry about that much
  65. # product category
  66. try:
  67. temp = rows[1].find('strong').text
  68. category = cleanString(temp.strip())
  69. except:
  70. category = "-1"
  71. print('category')
  72. # product number of views
  73. views = "-1"
  74. reviews = "-1" # 9 Product_Number_Of_Reviews
  75. rating_item = "-1" # 10 Product_Rating
  76. addDate = "-1" # 11 Product_AddedDate
  77. # BTC selling price box box-rounded mt-2
  78. BTC = "-1"
  79. # USD selling price
  80. try:
  81. temp = rows[2].find('strong').text
  82. if " $" in temp:
  83. temp = temp.replace(" $", "")
  84. elif "$" in temp:
  85. temp = temp.replace("$", "")
  86. USD = cleanString((temp.strip()))
  87. except:
  88. try:
  89. temp = soup.find('li').find('strong').text
  90. if " $" in temp:
  91. temp = temp.replace(" $", "")
  92. elif "$" in temp:
  93. temp = temp.replace("$", "")
  94. USD = cleanString((temp.strip()))
  95. except:
  96. print("USD")
  97. EURO = "-1" # 14 Product_EURO_SellingPrice
  98. # product sold
  99. try:
  100. if (len(rows) <= 5):
  101. temp = rows[4].find('td').text
  102. string = cleanString(temp)
  103. if (string == 'Left/Sold'):
  104. temp = rows[4].findAll('td')
  105. temp = temp[1].findAll('span')
  106. # left
  107. temp2 = temp[1].text
  108. temp3 = temp[1].text
  109. if(" items" in temp2):
  110. temp2 = temp2.replace(" items", "")
  111. if(" items" in temp3):
  112. temp3 = temp3.replace(" items", "")
  113. sold = (cleanString(temp2.strip()))
  114. left = cleanString(temp3.strip())
  115. else:
  116. sold = '-1'
  117. left = "-1"
  118. else:
  119. sold = '-1'
  120. left = "-1"
  121. except:
  122. print("success")
  123. sold = '-1'
  124. left = "-1"
  125. shipFrom = "-1" # 17 Product_ShippedFrom
  126. shipTo = "-1" # 18 Product_ShippedTo
  127. # Populating the final variable (this should be a list with all fields scraped)
  128. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  129. BTC, USD, EURO, sold, left, shipFrom, shipTo)
  130. # Sending the results
  131. return row
  132. #parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  133. #stores info it needs in different lists, these lists are returned after being organized
  134. #@param: soup object looking at html page of listing page
  135. #return: 'row' that contains a variety of lists that each hold info on the listing page
  136. def lionmarketplace_listing_parser(soup):
  137. # Fields to be parsed
  138. nm = 0 # *Total_Products (Should be Integer)
  139. mktName = "M00nkeyMarket" # 0 *Marketplace_Name
  140. vendor = [] # 1 *Vendor y
  141. rating_vendor = [] # 2 Vendor_Rating
  142. success = [] # 3 Vendor_Successful_Transactions
  143. name = [] # 4 *Product_Name y
  144. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about this
  145. MS = [] # 6 Product_MS_Classification (Microsoft Security) dont worry about this
  146. category = [] # 7 Product_Category y
  147. describe = [] # 8 Product_Description
  148. views = [] # 9 Product_Number_Of_Views
  149. reviews = [] # 10 Product_Number_Of_Reviews
  150. rating_item = [] # 11 Product_Rating
  151. addDate = [] # 12 Product_AddDate
  152. BTC = [] # 13 Product_BTC_SellingPrice
  153. USD = [] # 14 Product_USD_SellingPrice y
  154. EURO = [] # 15 Product_EURO_SellingPrice
  155. sold = [] # 16 Product_QuantitySold
  156. qLeft = [] # 17 Product_QuantityLeft
  157. shipFrom = [] # 18 Product_ShippedFrom
  158. shipTo = [] # 19 Product_ShippedTo
  159. href = [] # 20 Product_Links
  160. listing = soup.findAll('div', {"class": "card-body"})
  161. # Populating the Number of Products
  162. nm = len(listing)
  163. for a in listing:
  164. row = a.findAll('p')
  165. # vendor
  166. try:
  167. temp = row[3].text
  168. vendor.append(cleanString(temp.strip()))
  169. except:
  170. vendor.append("-1")
  171. print('vendor')
  172. # vendor rating
  173. rating_vendor.append("-1")
  174. # successful transactions CHECK AGAIN HERE
  175. success.append("-1")
  176. # product name
  177. try:
  178. temp = a.find('a').text
  179. name.append(cleanString(temp.strip()))
  180. except:
  181. name.append("-1")
  182. print('product name')
  183. CVE.append('-1')
  184. MS.append('-1')
  185. # product category
  186. try:
  187. temp = row[2].text
  188. if "Category: " in temp:
  189. temp = temp.replace("Category: ", "")
  190. category.append(cleanString(temp.strip()))
  191. except:
  192. print("Error in product category")
  193. describe.append('-1')
  194. # product views
  195. views.append("-1")
  196. reviews.append('-1') # 10 Product_Number_Of_Reviews
  197. rating_item.append('-1') # 11 Product_Rating
  198. addDate.append('-1') # 12 Product_AddDate
  199. # BTC
  200. BTC.append('-1')
  201. # USD
  202. try:
  203. temp = row[0].find('strong').text
  204. if ' $' in temp:
  205. temp = temp.replace(" $", "")
  206. USD.append(cleanString(temp.strip())) # 14 Product_USD_SellingPrice
  207. except:
  208. print("USD")
  209. USD.append("-1")
  210. EURO.append("-1") # 15 Product_EURO_SellingPrice
  211. # product sold
  212. sold.append("-1")
  213. qLeft.append('-1') # 17 Product_QuantityLeft
  214. shipFrom.append('-1') # 18 Product_ShippedFrom
  215. shipTo.append('-1') # 19 Product_ShippedTo
  216. # href
  217. try:
  218. temp = a.find('a').get('href')
  219. href.append(temp)
  220. except:
  221. print('product name')
  222. # Populate the final variable (this should be a list with all fields scraped)
  223. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  224. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href)
  225. #called by the crawler to get description links on a listing page
  226. #@param: beautifulsoup object that is using the correct html page (listing page)
  227. #return: list of description links from a listing page
  228. def lionmarketplace_links_parser(soup):
  229. # Returning all links that should be visited by the Crawler
  230. href = []
  231. listing = soup.findAll('div', {"class": "container d-flex justify-content-center"})
  232. for a in listing:
  233. bae = a.find('a', href=True)
  234. link = bae['href']
  235. href.append(link)
  236. return href