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.

281 lines
11 KiB

  1. __author__ = 'DarkWeb'
  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 torbay_description_parser(soup):
  11. # Fields to be parsed
  12. name = "-1" # 0 Product_Name
  13. describe = "-1" # 1 Product_Description
  14. lastSeen = "-1" # 2 Product_LastViewDate
  15. rules = "-1" # 3 NOT USED ...
  16. CVE = "-1" # 4 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  17. MS = "-1" # 5 Product_MS_Classification (Microsoft Security)
  18. review = "-1" # 6 Product_Number_Of_Reviews
  19. category = "-1" # 7 Product_Category
  20. shipFrom = "-1" # 8 Product_ShippedFrom
  21. shipTo = "-1" # 9 Product_ShippedTo
  22. left = "-1" # 10 Product_QuantityLeft
  23. escrow = "-1" # 11 Vendor_Warranty
  24. terms = "-1" # 12 Vendor_TermsAndConditions
  25. vendor = "-1" # 13 Vendor_Name
  26. sold = "-1" # 14 Product_QuantitySold
  27. addDate = "-1" # 15 Product_AddedDate
  28. available = "-1" # 16 NOT USED ...
  29. endDate = "-1" # 17 NOT USED ...
  30. BTC = "-1" # 18 Product_BTC_SellingPrice
  31. USD = "-1" # 19 Product_USD_SellingPrice
  32. rating = "-1" # 20 Vendor_Rating
  33. success = "-1" # 21 Vendor_Successful_Transactions
  34. EURO = "-1" # 22 Product_EURO_SellingPrice
  35. # Finding Product Name
  36. name = soup.find('div', {'class': 'product-information'}).find('h1').text.strip()
  37. # Finding Vendor
  38. vendor = soup.find('div', {"class": "profile-info"}).find('a').text.strip()
  39. # Finding Vendor Rating
  40. rating.append(-1)
  41. # Finding Successful Transactions
  42. success.append(-1)
  43. bae = soup.find('div', {'class': "box"}).find_all('ul')
  44. # Finding Prices
  45. USD = soup.find('div', {'class': "total-price"}).find('span').text.strip()
  46. # Finding Escrow NEED WORK
  47. escrow = li[0].find('span', {'class': "tag is-dark"}).text.strip()
  48. # Finding the Product Category
  49. category = soup.find('div', {'class': "profile-info"}).find('p').find('a').text.strip()
  50. # Finding the Product Quantity Available
  51. left.append(-1)
  52. # Finding Number Sold
  53. sold.append(-1)
  54. li = bae[3].find_all('li')
  55. # Finding Shipment Information (Origin)
  56. if "Ships from:" in li[-2].text:
  57. shipFrom = li[-2].text
  58. shipFrom = shipFrom.replace("Ships from: ", "")
  59. # shipFrom = shipFrom.replace(",", "")
  60. shipFrom = shipFrom.strip()
  61. # Finding Shipment Information (Destination)
  62. shipTo = li[-1].find('div', {'title': "List of countries is scrollable"}).text
  63. shipTo = shipTo.replace("Ships to: ", "")
  64. shipTo = shipTo.strip()
  65. if "certain countries" in shipTo:
  66. countries = ""
  67. tags = li[-1].find_all('span', {'class': "tag"})
  68. for tag in tags:
  69. country = tag.text.strip()
  70. countries += country + ", "
  71. shipTo = countries.strip(", ")
  72. # Finding the Product description
  73. describe = soup.find('div', {'class': "pre-line"}).text
  74. describe = describe.replace("\n", " ")
  75. describe = describe.strip()
  76. '''# Finding the Number of Product Reviews
  77. tag = soup.findAll(text=re.compile('Reviews'))
  78. for index in tag:
  79. reviews = index
  80. par = reviews.find('(')
  81. if par >=0:
  82. reviews = reviews.replace("Reviews (","")
  83. reviews = reviews.replace(")","")
  84. reviews = reviews.split(",")
  85. review = str(abs(int(reviews[0])) + abs(int(reviews[1])))
  86. else :
  87. review = "-1"'''
  88. # Searching for CVE and MS categories
  89. cve = soup.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  90. if cve:
  91. CVE = " "
  92. for idx in cve:
  93. CVE += (idx)
  94. CVE += " "
  95. CVE = CVE.replace(',', ' ')
  96. CVE = CVE.replace('\n', '')
  97. ms = soup.findAll(text=re.compile('MS\d{2}-\d{3}'))
  98. if ms:
  99. MS = " "
  100. for im in ms:
  101. MS += (im)
  102. MS += " "
  103. MS = MS.replace(',', ' ')
  104. MS = MS.replace('\n', '')
  105. # Populating the final variable (this should be a list with all fields scraped)
  106. row = (name, describe, lastSeen, rules, CVE, MS, review, category, shipFrom, shipTo, left, escrow, terms, vendor,
  107. sold, addDate, available, endDate, BTC, USD, rating, success, EURO)
  108. # Sending the results
  109. return row
  110. #parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  111. #stores info it needs in different lists, these lists are returned after being organized
  112. #@param: soup object looking at html page of listing page
  113. #return: 'row' that contains a variety of lists that each hold info on the listing page
  114. def torbay_listing_parser(soup):
  115. # Fields to be parsed
  116. nm = 0 # Total_Products (Should be Integer)
  117. mktName = "TorBay" # 0 Marketplace_Name
  118. name = [] # 1 Product_Name
  119. CVE = [] # 2 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  120. MS = [] # 3 Product_MS_Classification (Microsoft Security)
  121. category = [] # 4 Product_Category
  122. describe = [] # 5 Product_Description
  123. escrow = [] # 6 Vendor_Warranty
  124. views = [] # 7 Product_Number_Of_Views
  125. reviews = [] # 8 Product_Number_Of_Reviews
  126. addDate = [] # 9 Product_AddDate
  127. lastSeen = [] # 10 Product_LastViewDate
  128. BTC = [] # 11 Product_BTC_SellingPrice
  129. USD = [] # 12 Product_USD_SellingPrice
  130. EURO = [] # 13 Product_EURO_SellingPrice
  131. sold = [] # 14 Product_QuantitySold
  132. qLeft =[] # 15 Product_QuantityLeft
  133. shipFrom = [] # 16 Product_ShippedFrom
  134. shipTo = [] # 17 Product_ShippedTo
  135. vendor = [] # 18 Vendor
  136. rating = [] # 19 Vendor_Rating
  137. success = [] # 20 Vendor_Successful_Transactions
  138. href = [] # 23 Product_Links (Urls)
  139. listing = soup.findAll('div', {"class": "product-card"})
  140. # Populating the Number of Products
  141. nm = len(listing)
  142. for a in listing:
  143. bae = a.findAll('a', href=True)
  144. # Adding the url to the list of urls
  145. link = bae[0].get('href')
  146. link = cleanLink(link)
  147. href.append(link)
  148. # Finding the Product
  149. product = bae[1].find('p').text
  150. product = product.replace('\n', ' ')
  151. product = product.replace(",", "")
  152. product = product.replace("...", "")
  153. product = product.strip()
  154. name.append(product)
  155. bae = a.find('div', {'class': "media-content"}).find('div').find_all('div')
  156. if len(bae) >= 5:
  157. # Finding Prices
  158. price = bae[0].text
  159. ud = price.replace(" USD", " ")
  160. # u = ud.replace("$","")
  161. u = ud.replace(",", "")
  162. u = u.strip()
  163. USD.append(u)
  164. # bc = (prc[1]).strip(' BTC')
  165. # BTC.append(bc)
  166. # Finding the Vendor
  167. vendor_name = bae[1].find('a').text
  168. vendor_name = vendor_name.replace(",", "")
  169. vendor_name = vendor_name.strip()
  170. vendor.append(vendor_name)
  171. # Finding the Category
  172. cat = bae[2].find('small').text
  173. cat = cat.replace("Category: ", "")
  174. cat = cat.replace(",", "")
  175. cat = cat.strip()
  176. category.append(cat)
  177. # Finding Number Sold and Quantity Left
  178. num = bae[3].text
  179. num = num.replace("Sold: ", "")
  180. num = num.strip()
  181. sold.append(num)
  182. quant = bae[4].find('small').text
  183. quant = quant.replace("In stock: ", "")
  184. quant = quant.strip()
  185. qLeft.append(quant)
  186. # Finding Successful Transactions
  187. freq = bae[1].text
  188. freq = freq.replace(vendor_name, "")
  189. freq = re.sub(r'Vendor Level \d+', "", freq)
  190. freq = freq.replace("(", "")
  191. freq = freq.replace(")", "")
  192. freq = freq.strip()
  193. success.append(freq)
  194. # Searching for CVE and MS categories
  195. cve = a.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  196. if not cve:
  197. cveValue="-1"
  198. else:
  199. cee = " "
  200. for idx in cve:
  201. cee += (idx)
  202. cee += " "
  203. cee = cee.replace(',', ' ')
  204. cee = cee.replace('\n', '')
  205. cveValue=cee
  206. CVE.append(cveValue)
  207. ms = a.findAll(text=re.compile('MS\d{2}-\d{3}'))
  208. if not ms:
  209. MSValue="-1"
  210. else:
  211. me = " "
  212. for im in ms:
  213. me += (im)
  214. me += " "
  215. me = me.replace(',', ' ')
  216. me = me.replace('\n', '')
  217. MSValue=me
  218. MS.append(MSValue)
  219. # Populate the final variable (this should be a list with all fields scraped)
  220. return organizeProducts(mktName, nm, name, CVE, MS, category, describe, escrow, views, reviews, addDate, lastSeen,
  221. BTC, USD, EURO, qLeft, shipFrom, shipTo, vendor, rating, success, sold, href)
  222. #called by the crawler to get description links on a listing page
  223. #@param: beautifulsoup object that is using the correct html page (listing page)
  224. #return: list of description links from a listing page
  225. def torbay_links_parser(soup):
  226. # Returning all links that should be visited by the Crawler
  227. href = []
  228. listing = soup.find('section', {"id": "content"}).findAll('div', {"class": "product-card"})
  229. for a in listing:
  230. bae = a.find('div', {"class": "pc-footer"}).find('a', {"class": "btn btn-primary"}, href=True)
  231. link = bae['href']
  232. href.append(link)
  233. return href