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.

301 lines
12 KiB

  1. __author__ = 'cern'
  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 blackpyramid_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)
  18. MS = "-1" # 6 Product_MS_Classification (Microsoft Security)
  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. image = "-1" # 19 Product_Image
  32. vendor_image = "-1" # 20 Vendor_Image
  33. # Finding Product Name
  34. name = soup.find('div', {'class': 'panel39002'}).find('span').next_sibling
  35. name = name.replace('\n', ' ')
  36. name = name.replace(",", "")
  37. name = name.strip()
  38. # Finding Product Rating
  39. rating_span = soup.find('span', {'class': 'to3098503t'}).find_next_sibling('span')
  40. rating_num = rating_span.find('b').text
  41. if rating_num != 'N/A':
  42. rating_item = rating_num[0:3]
  43. # product description
  44. describe = soup.findAll('div', {'class': 'fer048953'})[1].text
  45. describe = describe.replace('\n', ' ')
  46. describe = describe.replace(",", "")
  47. describe = describe.strip()
  48. # Finding Vendor
  49. vendor = soup.find('div', {'class': 'bold03905 vstat364'}).text
  50. vendor = vendor.split(" ")
  51. vendor = vendor[2][:-1]
  52. vendor = vendor.replace('\n', ' ')
  53. vendor = vendor.replace(",", "")
  54. vendor = vendor.strip()
  55. # Finding Product Rating
  56. rating_div = soup.find('div', {'class': 'bold03905 vstat364'}).find_next_sibling('div').find_next_sibling('div')
  57. rating_vendor = cleanNumbers(rating_div.text)
  58. if rating_vendor == "":
  59. rating_vendor = "-1"
  60. # Finding Successful Transactions
  61. success_container = soup.find('ul', {'class': 'ul3o00953'}).findAll('li')[1]
  62. success = success_container.find('div').text
  63. success = success.replace('"', '')
  64. success = success.replace("\n", " ")
  65. success = success.replace(",", "")
  66. success = success.strip()
  67. # Finding Prices
  68. USD_text = soup.find('li', {'class': 'vul2994 vghul995'}).find('div').text
  69. USD = USD_text.split(',')[1]
  70. USD = USD.replace('\n', ' ')
  71. USD = USD.replace(",", "")
  72. USD = USD.strip()
  73. container = soup.find('ul', {'class': 'bic03095'})
  74. # Finding Number Sold
  75. sold_container = container.find('li')
  76. sold_div = sold_container.findAll('div')[2]
  77. sold = sold_div.find('b').next_sibling
  78. sold = sold.replace('"', '')
  79. sold = sold.replace("\n", " ")
  80. sold = sold.replace(",", "")
  81. sold = sold.strip()
  82. # Finding the Product Quantity Available
  83. left_container = container.find('li')
  84. left_div = left_container.findAll('div')[3]
  85. left = left_div.find('b').next_sibling
  86. left = left.replace('"', '')
  87. left = left.replace("\n", " ")
  88. left = left.replace(",", "")
  89. left = left.strip()
  90. # Finding number of reviews
  91. positive = soup.find('span', {'class': 'ar04999324'}).text
  92. neutral = soup.find('span', {'class': 'ti9400005 can39953'}).text
  93. negative = soup.find('span', {'class': 'ti9400005 ti90088 can39953'}).text
  94. reviews = int(positive) + int(neutral) + int(negative)
  95. # Finding product image
  96. image = soup.find('img', {'class': 'img0390503'})
  97. image = image.get('src')
  98. image = image.split('base64,')[-1]
  99. vendor_image = soup.find('img', {'class': 'img0390503'})
  100. vendor_image = vendor_image.get('src')
  101. vendor_image = vendor_image.split('base64,')[-1]
  102. # Searching for CVE and MS categories
  103. cve = soup.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  104. if cve:
  105. CVE = " "
  106. for idx in cve:
  107. CVE += (idx)
  108. CVE += " "
  109. CVE = CVE.replace(',', ' ')
  110. CVE = CVE.replace('\n', '')
  111. ms = soup.findAll(text=re.compile('MS\d{2}-\d{3}'))
  112. if ms:
  113. MS = " "
  114. for im in ms:
  115. MS += (im)
  116. MS += " "
  117. MS = MS.replace(',', ' ')
  118. MS = MS.replace('\n', '')
  119. # Populating the final variable (this should be a list with all fields scraped)
  120. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  121. BTC, USD, EURO, sold, left, shipFrom, shipTo, image, vendor_image)
  122. # Sending the results
  123. return row
  124. #parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  125. #stores info it needs in different lists, these lists are returned after being organized
  126. #@param: soup object looking at html page of listing page
  127. #return: 'row' that contains a variety of lists that each hold info on the listing page
  128. def blackpyramid_listing_parser(soup):
  129. # Fields to be parsed
  130. nm = 0 # *Total_Products (Should be Integer)
  131. mktName = "BlackPyramid" # 0 *Marketplace_Name
  132. vendor = [] # 1 *Vendor y
  133. rating_vendor = [] # 2 Vendor_Rating
  134. success = [] # 3 Vendor_Successful_Transactions
  135. name = [] # 4 *Product_Name y
  136. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about this
  137. MS = [] # 6 Product_MS_Classification (Microsoft Security) dont worry about this
  138. category = [] # 7 Product_Category y
  139. describe = [] # 8 Product_Description
  140. views = [] # 9 Product_Number_Of_Views
  141. reviews = [] # 10 Product_Number_Of_Reviews
  142. rating_item = [] # 11 Product_Rating
  143. addDate = [] # 12 Product_AddDate
  144. BTC = [] # 13 Product_BTC_SellingPrice
  145. USD = [] # 14 Product_USD_SellingPrice y
  146. EURO = [] # 15 Product_EURO_SellingPrice
  147. sold = [] # 16 Product_QuantitySold
  148. qLeft = [] # 17 Product_QuantityLeft
  149. shipFrom = [] # 18 Product_ShippedFrom
  150. shipTo = [] # 19 Product_ShippedTo
  151. image = [] # 20 Product_Image
  152. image_vendor = [] # 21 Vendor_Image
  153. href = [] # 22 Product_Links
  154. listing = soup.findAll('article', {"class": "product"})
  155. # Some listing pages have an additional article section which is blank
  156. if not listing[-1].findAll('a', href=True):
  157. listing = listing[:-1]
  158. # Populating the Number of Products
  159. nm = len(listing)
  160. for card in listing:
  161. bae = card.findAll('a', href=True)
  162. # Adding the url to the list of urls
  163. link = bae[2].get('href')
  164. href.append(link)
  165. # Finding the Product
  166. product = bae[3].text
  167. product = product.replace('\n', ' ')
  168. product = product.replace(",", "")
  169. product = product.replace("...", "")
  170. product = product.strip()
  171. name.append(product)
  172. # # Finding description
  173. # # 'recurisve = False' only searches direct children
  174. # desc = card.findChildren('div', recursive=False)[0]
  175. # desc = desc.findAll('div', recursive=False)[3].text
  176. # desc = desc.replace('\n', ' ')
  177. # desc = desc.replace(",", "")
  178. # desc = desc.strip()
  179. # describe.append(desc)
  180. # Finding Vendor Name
  181. vendor_name = bae[4].find('span').text
  182. vendor_name = vendor_name.split(' ')[1]
  183. vendor_name = vendor_name.replace('\n', ' ')
  184. vendor_name = vendor_name.replace(",", "")
  185. vendor_name = vendor_name.strip()
  186. vendor.append(vendor_name)
  187. # Finding the Category
  188. cat = card.findAll('div', recursive=False)[0].findAll('div', recursive=False)[1].find('span').text
  189. cat = cat.replace("\n", "")
  190. cat = cat.replace(",", "")
  191. cat = cat.strip()
  192. category.append(cat)
  193. bae = card.findAll('div', recursive=False)[1].findAll('div', recursive=False)[1]
  194. # Finding amount left
  195. left = bae.findAll('div', recursive=False)[1].text
  196. left = left.replace("x", "")
  197. left = left.replace('\n', ' ')
  198. left = left.replace(",", "")
  199. left = left.strip()
  200. qLeft.append(left)
  201. # Finding amount sold
  202. qsold = bae.findAll('div', recursive=False)[2].text
  203. qsold = qsold.replace('\n', ' ')
  204. qsold = qsold.replace("x", "")
  205. qsold = qsold.replace(",", "")
  206. qsold = qsold.strip()
  207. sold.append(qsold)
  208. # Finding product image
  209. product_image = card.find('img')
  210. product_image = product_image.get('src')
  211. product_image = product_image.split('base64,')[-1]
  212. image.append(product_image)
  213. # Searching for CVE and MS categories
  214. cve = card.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  215. if not cve:
  216. cveValue="-1"
  217. else:
  218. cee = " "
  219. for idx in cve:
  220. cee += (idx)
  221. cee += " "
  222. cee = cee.replace(',', ' ')
  223. cee = cee.replace('\n', '')
  224. cveValue=cee
  225. CVE.append(cveValue)
  226. ms = card.findAll(text=re.compile('MS\d{2}-\d{3}'))
  227. if not ms:
  228. MSValue="-1"
  229. else:
  230. me = " "
  231. for im in ms:
  232. me += (im)
  233. me += " "
  234. me = me.replace(',', ' ')
  235. me = me.replace('\n', '')
  236. MSValue=me
  237. MS.append(MSValue)
  238. # Populate the final variable (this should be a list with all fields scraped)
  239. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  240. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href, image,
  241. image_vendor)
  242. #called by the crawler to get description links on a listing page
  243. #@param: beautifulsoup object that is using the correct html page (listing page)
  244. #return: list of description links from a listing page
  245. def BlackPyramid_links_parser(soup):
  246. # Returning all links that should be visited by the Crawler
  247. href = []
  248. listing = soup.findAll('article', {"class": "product"})
  249. for item in listing:
  250. link = item.find('a', {"class": "ah39063"})['href']
  251. href.append(link)
  252. return href