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.

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