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.

327 lines
12 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 torzon_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': 'box rounded mb-0'}).find('a').text
  34. vendor = (cleanString(temp.strip()))
  35. except:
  36. vendor = "-1"
  37. #successful transaction
  38. try:
  39. temp = soup.findAll('div', {'class','text-center text-truncate column-flex ml-1 mr-1'}) #card sidebar-menu mb-4 card sidebar-menu mb-4
  40. temp2 = temp[1].findAll('span', {'class', 'float-right font-weight-bold'})
  41. temp = temp2[1].text
  42. success = (temp.strip())
  43. except:
  44. print("success")
  45. #vendor rating 5
  46. try:
  47. temp = soup.findAll('div', {'class', 'text-center text-truncate column-flex ml-1 mr-1'}) # card sidebar-menu mb-4 card sidebar-menu mb-4
  48. temp2 = temp[1].findAll('span', {'class', 'float-right font-weight-bold'})
  49. temp = temp2[5].text
  50. rating_vendor = (cleanString(temp.strip()))
  51. except:
  52. print("product")
  53. # product name
  54. try:
  55. temp = soup.find('h3', {'class', 'h3 rounded card-title'}).find('span').text
  56. name = (cleanString(temp.strip()))
  57. except:
  58. temp = soup.find('h3', {'class', 'h3 rounded card-title'}).find('span').find("div").text
  59. name = (cleanString(temp.strip()))
  60. # product description
  61. describe = soup.find('div', {'class': "box rounded flex-fill"}).find('pre').text
  62. if "\n" in describe:
  63. describe = describe.replace("\n", " ")
  64. describe = describe.replace("\r", " ")
  65. describe = cleanString(describe.strip())
  66. CVE = "-1" # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about that much
  67. MS = "-1" # 6 Product_MS_Classification (Microsoft Security) dont worry about that much
  68. # product category
  69. try:
  70. temp = soup.findAll('table', {'class', 'table table-hover'})
  71. temp2 = temp[1].find('tr').findAll('td')
  72. temp = temp2[1].text
  73. category = cleanString(temp.strip())
  74. except:
  75. temp = soup.find('table', {'class', 'table table-hover'})
  76. temp2 = temp.find('tbody').find('tr').findAll('td')
  77. temp = temp2[1].text
  78. category = cleanString(temp.strip())
  79. # product number of view
  80. try:
  81. temp = soup.find('div', {'class', 'box rounded mb-0'})
  82. temp2 = temp.findAll('i')
  83. temp = temp2[2].text
  84. views = cleanString((temp.strip()))
  85. except:
  86. print('Product number of view')
  87. reviews = "-1" # 9 Product_Number_Of_Reviews
  88. rating_item = "-1" # 10 Product_Rating
  89. addDate = "-1" # 11 Product_AddedDate
  90. #BTC selling price box box-rounded mt-2
  91. try:
  92. temp = soup.find('div', {'class', 'box box-rounded mt-2'})
  93. temp2 = temp.findAll('i', {'class', 'float-right color-prices'})
  94. temp = temp2[1].text
  95. BTC = cleanString((temp.strip()))
  96. except:
  97. try:
  98. temp = soup.find('div', {'class', 'box box-rounded'})
  99. temp2 = temp.findAll('span', {'class', 'float-right color-prices'})
  100. temp = temp2[1].text
  101. BTC = cleanString((temp.strip()))
  102. except:
  103. print("BTC")
  104. # USD selling price
  105. try:
  106. temp = soup.find('div', {'class', 'box box-rounded mt-2'})
  107. temp2 = temp.findAll('center')
  108. temp = temp2[1].find('i').text
  109. if "$" in temp:
  110. temp = temp.replace("$", "")
  111. USD = cleanString((temp.strip()))
  112. except:
  113. try:
  114. temp = soup.find('div', {'class', 'box box-rounded'})
  115. temp2 = temp.findAll('center')
  116. temp = temp2[1].find('span').text
  117. if "$" in temp:
  118. temp = temp.replace("$", "")
  119. USD = cleanString((temp.strip()))
  120. except:
  121. print("USD")
  122. EURO = "-1" # 14 Product_EURO_SellingPrice
  123. # product sold
  124. try:
  125. temp = soup.find('div', {'class', 'box rounded mb-0'}) # card sidebar-menu mb-4 card sidebar-menu mb-4
  126. temp2 = temp.find('i')
  127. temp = temp2.text
  128. sold = (cleanString(temp.strip()))
  129. # sold = "-1"
  130. except:
  131. print("product sold")
  132. # product quantatiy left ###ERRROR
  133. try:
  134. temp = soup.findAll('table', {'class', 'table table-hover'})
  135. temp2 = temp[1].findAll('tr')
  136. temp3 = temp2[1].findAll('td')
  137. temp = temp3[1].text
  138. left = cleanString(temp.strip())
  139. except:
  140. temp = soup.find('table', {'class', 'table table-hover'})
  141. temp2 = temp.findAll('tr')
  142. temp3 = temp2[1].findAll('td')
  143. temp = temp3[1].text
  144. left = cleanString(temp.strip())
  145. shipFrom = "-1" # 17 Product_ShippedFrom
  146. shipTo = "-1" # 18 Product_ShippedTo
  147. # Populating the final variable (this should be a list with all fields scraped)
  148. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  149. BTC, USD, EURO, sold, left, shipFrom, shipTo)
  150. # Sending the results
  151. return row
  152. #parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  153. #stores info it needs in different lists, these lists are returned after being organized
  154. #@param: soup object looking at html page of listing page
  155. #return: 'row' that contains a variety of lists that each hold info on the listing page
  156. def torzon_listing_parser(soup):
  157. # Fields to be parsed
  158. nm = 0 # *Total_Products (Should be Integer)
  159. mktName = "Torzon" # 0 *Marketplace_Name
  160. vendor = [] # 1 *Vendor y
  161. rating_vendor = [] # 2 Vendor_Rating
  162. success = [] # 3 Vendor_Successful_Transactions
  163. name = [] # 4 *Product_Name y
  164. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about this
  165. MS = [] # 6 Product_MS_Classification (Microsoft Security) dont worry about this
  166. category = [] # 7 Product_Category y
  167. describe = [] # 8 Product_Description
  168. views = [] # 9 Product_Number_Of_Views
  169. reviews = [] # 10 Product_Number_Of_Reviews
  170. rating_item = [] # 11 Product_Rating
  171. addDate = [] # 12 Product_AddDate
  172. BTC = [] # 13 Product_BTC_SellingPrice
  173. USD = [] # 14 Product_USD_SellingPrice y
  174. EURO = [] # 15 Product_EURO_SellingPrice
  175. sold = [] # 16 Product_QuantitySold
  176. qLeft = [] # 17 Product_QuantityLeft
  177. shipFrom = [] # 18 Product_ShippedFrom
  178. shipTo = [] # 19 Product_ShippedTo
  179. href = [] # 20 Product_Links
  180. listing = soup.findAll('div', {"class": "card mt-1"})
  181. # Populating the Number of Products
  182. nm = len(listing)
  183. for a in listing:
  184. # vendor
  185. try:
  186. temp = a.find('div', {'class','col-5 justify-content-between mx-auto'}).find('a').text
  187. vendor.append(cleanString(temp.strip()))
  188. except:
  189. print('vendor')
  190. #vendor rating
  191. #successful transactions CHECK AGAIN HERE
  192. try:
  193. success.append("-1")
  194. except:
  195. print('successful transactions')
  196. # product name
  197. try:
  198. temp = a.find('h5', {'class','card-title rounded text-truncate'}).find('a').text
  199. name.append(cleanString(temp.strip()))
  200. except:
  201. print('product name')
  202. CVE.append('-1')
  203. MS.append('-1')
  204. rating_vendor.append('-1')
  205. # product category
  206. try:
  207. temp = soup.find('div', {'class', 'card-sidebar-menu box mb-2 flex-column'}).find('h3').find('span').text
  208. if "Search Results for: " in temp:
  209. temp = temp.replace("Search Results for: ", "")
  210. category.append(cleanString(temp.strip()))
  211. except:
  212. print("Error in product category")
  213. describe.append('-1')
  214. # product views
  215. try:
  216. temp = a.find('h6',{'class', 'card-subtitle mb-1 text-muted text-truncate'})
  217. temp2 = temp.find('i').text
  218. views.append(cleanString(temp2.strip()))
  219. except:
  220. print("Error in views")
  221. reviews.append('-1') # 10 Product_Number_Of_Reviews
  222. rating_item.append('-1') # 11 Product_Rating
  223. addDate.append('-1') # 12 Product_AddDate
  224. # BTC
  225. try:
  226. temp = a.find('div', {'class', 'col-3 justify-content-between mx-auto'})
  227. temp2 = temp.findAll('p')
  228. temp = temp2[1].text
  229. BTC.append(cleanString(temp.strip()))
  230. except:
  231. print("BTC")
  232. #USD ERROR get rid of $
  233. try:
  234. temp = a.find('div', {'class', 'col-12 justify-content-between mx-auto'}).find('i').text
  235. if '$' in temp:
  236. temp = temp.replace("$", "")
  237. USD.append(cleanString(temp.strip())) # 14 Product_USD_SellingPrice
  238. except:
  239. print("USD")
  240. EURO.append("-1") # 15 Product_EURO_SellingPrice
  241. #product sold
  242. try:
  243. temp = a.find('div', {'class', 'col-12 mx-auto text-truncate text-center flex-fill'}).findAll('p', {'class', 'card-text mb-0'})
  244. temp2 = temp[1].find('i').text
  245. sold.append(cleanString(temp2.strip()))
  246. except:
  247. print("product sold")
  248. qLeft.append('-1') # 17 Product_QuantityLeft
  249. shipFrom.append('-1') # 18 Product_ShippedFrom
  250. shipTo.append('-1') # 19 Product_ShippedTo
  251. #href
  252. try:
  253. temp = a.find('h5', {'class', 'card-title rounded text-truncate'}).find('a').get('href')
  254. href.append(temp) # 20 Product_Links
  255. except:
  256. print("href")
  257. # Populate the final variable (this should be a list with all fields scraped)
  258. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  259. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href)
  260. #called by the crawler to get description links on a listing page
  261. #@param: beautifulsoup object that is using the correct html page (listing page)
  262. #return: list of description links from a listing page
  263. def torzon_links_parser(soup):
  264. # Returning all links that should be visited by the Crawler
  265. href = []
  266. # listing = soup.findAll('div', {"class": "card mt-1"})
  267. listing = soup.find('td', {"valign": "top"}).find("tbody").findAll('td', {'width': '50%'})
  268. for a in listing:
  269. bae = a.find('a', href=True)#card-title rounded text-truncate
  270. link = bae['href']
  271. href.append(link)
  272. return href