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.

248 lines
9.2 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 city_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. divmd7 = soup.find('div', {'class': "col-md-7"})
  36. ptag = soup.findAll('p')
  37. # Finding Product Name
  38. # NA
  39. # Finding Vendor
  40. vendor = divmd7.find('a').text.strip()
  41. # Finding Vendor Rating
  42. # NA
  43. # Finding Successful Transactions
  44. success = soup.find('span', {'class': "badge-primary"})
  45. # Finding Prices
  46. USD = soup.find('span', {'class': "total"}).text.strip()
  47. BTC = soup.find('div', {'class': "text-center"}).text.strip()
  48. # Finding Escrow
  49. escrow = ptag[-1].text.strip()
  50. # Finding the Product Category
  51. category = ptag[-2].text.strip()
  52. # Finding the Product Quantity Available
  53. # NA
  54. # Finding Number Sold
  55. # NA
  56. # Finding Shipment Information (Origin)
  57. # NA
  58. # Finding Shipment Information (Destination)
  59. # NA
  60. # Finding the Product description
  61. describe = soup.find('div', {'class': "text-white"}).text
  62. describe = describe.replace("\n", " ")
  63. describe = describe.strip()
  64. '''# Finding the Number of Product Reviews
  65. tag = soup.findAll(text=re.compile('Reviews'))
  66. for index in tag:
  67. reviews = index
  68. par = reviews.find('(')
  69. if par >=0:
  70. reviews = reviews.replace("Reviews (","")
  71. reviews = reviews.replace(")","")
  72. reviews = reviews.split(",")
  73. review = str(abs(int(reviews[0])) + abs(int(reviews[1])))
  74. else :
  75. review = "-1"'''
  76. # Searching for CVE and MS categories
  77. cve = soup.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  78. if cve:
  79. CVE = " "
  80. for idx in cve:
  81. CVE += (idx)
  82. CVE += " "
  83. CVE = CVE.replace(',', ' ')
  84. CVE = CVE.replace('\n', '')
  85. ms = soup.findAll(text=re.compile('MS\d{2}-\d{3}'))
  86. if ms:
  87. MS = " "
  88. for im in ms:
  89. MS += (im)
  90. MS += " "
  91. MS = MS.replace(',', ' ')
  92. MS = MS.replace('\n', '')
  93. # Populating the final variable (this should be a list with all fields scraped)
  94. row = (name, describe, lastSeen, rules, CVE, MS, review, category, shipFrom, shipTo, left, escrow, terms, vendor,
  95. sold, addDate, available, endDate, BTC, USD, rating, success, EURO)
  96. # Sending the results
  97. return row
  98. #parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  99. #stores info it needs in different lists, these lists are returned after being organized
  100. #@param: soup object looking at html page of listing page
  101. #return: 'row' that contains a variety of lists that each hold info on the listing page
  102. def city_listing_parser(soup):
  103. # Fields to be parsed
  104. nm = 0 # Total_Products (Should be Integer)
  105. mktName = "CityMarket" # 0 Marketplace_Name
  106. name = [] # 1 Product_Name
  107. CVE = [] # 2 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  108. MS = [] # 3 Product_MS_Classification (Microsoft Security)
  109. category = [] # 4 Product_Category
  110. describe = [] # 5 Product_Description
  111. escrow = [] # 6 Vendor_Warranty
  112. views = [] # 7 Product_Number_Of_Views
  113. reviews = [] # 8 Product_Number_Of_Reviews
  114. addDate = [] # 9 Product_AddDate
  115. lastSeen = [] # 10 Product_LastViewDate
  116. BTC = [] # 11 Product_BTC_SellingPrice
  117. USD = [] # 12 Product_USD_SellingPrice
  118. EURO = [] # 13 Product_EURO_SellingPrice
  119. sold = [] # 14 Product_QuantitySold
  120. qLeft =[] # 15 Product_QuantityLeft
  121. shipFrom = [] # 16 Product_ShippedFrom
  122. shipTo = [] # 17 Product_ShippedTo
  123. vendor = [] # 18 Vendor
  124. rating = [] # 19 Vendor_Rating
  125. success = [] # 20 Vendor_Successful_Transactions
  126. href = [] # 23 Product_Links (Urls)
  127. listing = soup.findAll('div', {"class": "card"})
  128. # Populating the Number of Products
  129. nm = len(listing)
  130. for a in listing:
  131. bae = a.findAll('a', href=True)
  132. # Adding the url to the list of urls
  133. link = bae[0].get('href')
  134. link = cleanLink(link)
  135. href.append(link)
  136. # Finding the Product
  137. product = a.find('h4', {"class": "text-center"}).text
  138. product = product.replace('\n', ' ')
  139. product = product.replace(",", "")
  140. product = product.replace("...", "")
  141. product = product.strip()
  142. name.append(product)
  143. bae = a.find('div', {'class': "media-content"}).find('div').find_all('div')
  144. # Finding Prices
  145. price = a.find('div', {"class": "price"}).text
  146. tempUSD = price.split("~")[0]
  147. tempUSD = tempUSD.replace("$", "")
  148. tempUSD = tempUSD.strip()
  149. USD.append(tempUSD)
  150. tempBTC = price.split("~")[1]
  151. tempBTC = tempBTC.replace("BTC", "")
  152. tempBTC = tempBTC.strip()
  153. BTC.append(tempBTC)
  154. # Finding the Vendor
  155. # NA
  156. # Finding the Category
  157. # NA
  158. # Finding Number Sold and Quantity Left
  159. # NA
  160. # Finding Successful Transactions
  161. # NA
  162. # Searching for CVE and MS categories
  163. cve = a.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  164. if not cve:
  165. cveValue="-1"
  166. else:
  167. cee = " "
  168. for idx in cve:
  169. cee += (idx)
  170. cee += " "
  171. cee = cee.replace(',', ' ')
  172. cee = cee.replace('\n', '')
  173. cveValue=cee
  174. CVE.append(cveValue)
  175. ms = a.findAll(text=re.compile('MS\d{2}-\d{3}'))
  176. if not ms:
  177. MSValue="-1"
  178. else:
  179. me = " "
  180. for im in ms:
  181. me += (im)
  182. me += " "
  183. me = me.replace(',', ' ')
  184. me = me.replace('\n', '')
  185. MSValue=me
  186. MS.append(MSValue)
  187. # Populate the final variable (this should be a list with all fields scraped)
  188. return organizeProducts(mktName, nm, name, CVE, MS, category, describe, escrow, views, reviews, addDate, lastSeen,
  189. BTC, USD, EURO, qLeft, shipFrom, shipTo, vendor, rating, success, sold, href)
  190. #called by the crawler to get description links on a listing page
  191. #@param: beautifulsoup object that is using the correct html page (listing page)
  192. #return: list of description links from a listing page
  193. def city_links_parser(soup):
  194. # Returning all links that should be visited by the Crawler
  195. href = []
  196. listing = soup.findAll('div', {"class": "p-4"})
  197. for a in listing:
  198. bae = a.find('a', href=True)
  199. link = bae['href']
  200. href.append(link)
  201. return href