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.

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