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.

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