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.

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