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.

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