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.

294 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 gofish_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. divmb = soup.find('div', {'class': "p-3 mb-1 fs-3 fw-bold border border-2 bg-white rounded"})
  35. name = divmb.text
  36. name = name.replace('\n', ' ')
  37. name = name.replace(",", "")
  38. name = name.strip()
  39. # Finding Vendor
  40. vendor = soup.find('div', {'class': 'my-1'}).text.strip()
  41. # Finding Vendor Rating
  42. # temp = soup.find('div', {'class': ""}).text
  43. # temp = temp.split('(')
  44. # rating = temp[0].replace("Vendor's Review : ", "")
  45. # rating = rating.replace("%", "")
  46. # rating_vendor = rating.strip()
  47. # Finding the Product Rating and Number of Product Reviews
  48. # reviews = temp[2].replace(" review)", "")
  49. # reviews = reviews.strip()
  50. # temp = temp[1].split(")")
  51. # rating = temp[1].replace("Product Review : ", "")
  52. # rating = rating.replace("%", "")
  53. # rating_item = rating.strip()
  54. # Finding Prices
  55. precios = soup.findall('td', {'class': "text-end text-nowrap"})
  56. USD = precios[0].text.strip().replace('$', '')
  57. # Finding the Product Category
  58. # pmb = soup.findAll('p', {'class': "mb-1"})
  59. # category = pmb[-1].text
  60. # category = category.replace("Category: ", "").strip()
  61. # Finding the Product Quantity Available
  62. # left = divmb[-1].text
  63. # left = left.split(",", 1)[1]
  64. # left = left.replace("in stock", "")
  65. # left = left.strip()
  66. # Finding Number Sold
  67. # sold = divmb[-1].text
  68. # sold = sold.split(",", 1)[0]
  69. # sold = sold.replace("sold", "")
  70. # sold = sold.strip()
  71. # Finding Shipment Information (Origin)
  72. origin = soup.findall('div', {'class': "p-3 mt-2 mb-3 border border-2 bg-white rounded"})
  73. origin = origin[0].text.strip()
  74. shipFrom = origin[1:].strip()
  75. # Finding Shipment Information (Destination)
  76. dest = soup.findall('div', {'class': 'p-3 mb-3 overflow-auto border border-2 bg-white rounded'})
  77. dest = dest[-1].text.strip()
  78. shipTo = dest[1:].strip()
  79. # Finding the Product description
  80. cardbody = soup.findAll('div', {'class': "p-3 mb-3 overflow-auto border border-2 bg-white rounded"})
  81. describe = cardbody[0].text
  82. describe = describe.replace('\n', ' ')
  83. describe = describe.strip()
  84. # Finding Product Image
  85. img = soup.findall('figure', {'class': 'image-feature'})[0]
  86. image = img.find('img', {'class': 'image-block rounded'})
  87. image = image.get('src')
  88. image = image.split('base64,')[-1]
  89. # Searching for CVE and MS categories
  90. cve = soup.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  91. if cve:
  92. CVE = " "
  93. for idx in cve:
  94. CVE += (idx)
  95. CVE += " "
  96. CVE = CVE.replace(',', ' ')
  97. CVE = CVE.replace('\n', '')
  98. ms = soup.findAll(text=re.compile('MS\d{2}-\d{3}'))
  99. if ms:
  100. MS = " "
  101. for im in ms:
  102. MS += (im)
  103. MS += " "
  104. MS = MS.replace(',', ' ')
  105. MS = MS.replace('\n', '')
  106. # Populating the final variable (this should be a list with all fields scraped)
  107. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  108. BTC, USD, EURO, sold, left, shipFrom, shipTo, image, vendor_image)
  109. # Sending the results
  110. return row
  111. # parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  112. # stores info it needs in different lists, these lists are returned after being organized
  113. # @param: soup object looking at html page of listing page
  114. # return: 'row' that contains a variety of lists that each hold info on the listing page
  115. def gofish_listing_parser(soup):
  116. # Fields to be parsed
  117. nm = 0 # *Total_Products (Should be Integer)
  118. mktName = "GoFish" # 0 *Marketplace_Name
  119. vendor = [] # 1 *Vendor y
  120. rating_vendor = [] # 2 Vendor_Rating
  121. success = [] # 3 Vendor_Successful_Transactions
  122. name = [] # 4 *Product_Name y
  123. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about this
  124. MS = [] # 6 Product_MS_Classification (Microsoft Security) dont worry about this
  125. category = [] # 7 Product_Category y
  126. describe = [] # 8 Product_Description
  127. views = [] # 9 Product_Number_Of_Views
  128. reviews = [] # 10 Product_Number_Of_Reviews
  129. rating_item = [] # 11 Product_Rating
  130. addDate = [] # 12 Product_AddDate
  131. BTC = [] # 13 Product_BTC_SellingPrice
  132. USD = [] # 14 Product_USD_SellingPrice y
  133. EURO = [] # 15 Product_EURO_SellingPrice
  134. sold = [] # 16 Product_QuantitySold
  135. qLeft = [] # 17 Product_QuantityLeft
  136. shipFrom = [] # 18 Product_ShippedFrom
  137. shipTo = [] # 19 Product_ShippedTo
  138. image = [] # 20 Product_Image
  139. image_vendor = [] # 21 Vendor_Image
  140. href = [] # 22 Product_Links
  141. listing = soup.find('tbody', {"class": "border border-2 align-middle"}).findall('tr')
  142. # Populating the Number of Products
  143. nm = len(listing)
  144. for a in listing:
  145. bae = a.findAll('a', href=True)
  146. # Adding the url to the list of urls
  147. link = bae[0].get('href')
  148. href.append(link)
  149. # Finding the Product
  150. product = bae[1].text
  151. product = product.replace('\n', ' ')
  152. product = product.replace(",", "")
  153. product = product.replace("...", "")
  154. product = product.strip()
  155. name.append(product)
  156. # Finding Product Image
  157. product_image = bae[0].find('img')
  158. product_image = product_image.get('src')
  159. product_image = product_image.split('base64,')[-1]
  160. image.append(product_image)
  161. # Finding Prices
  162. price = a.find('span', {"class": "fw-bold text-nowrap"}).text
  163. price = price.replace("$","")
  164. price = price.strip()
  165. USD.append(price)
  166. # Finding the Vendor
  167. vendor_name = bae[-1].text
  168. vendor_name = vendor_name.replace(",", "")
  169. vendor_name = vendor_name.strip()
  170. vendor.append(vendor_name)
  171. image_vendor.append("-1")
  172. # Finding the Category
  173. # cat = lb[-1].find("span").text
  174. # cat = cat.replace("class:", "")
  175. # cat = cat.strip()
  176. # category.append(cat)
  177. # span = lb[0].findAll("span")
  178. # Finding Number of Views
  179. # num = span[0].text
  180. # num = num.replace("views:", "")
  181. # num = num.strip()
  182. # sold.append(num)
  183. # Finding Number Sold
  184. # num = span[2].text
  185. # num = num.replace("Sold:", "")
  186. # num = num.strip()
  187. # sold.append(num)
  188. # Finding Quantity Left
  189. # quant = span[1].text
  190. # quant = quant.replace("stock:", "")
  191. # quant = quant.strip()
  192. # qLeft.append(quant)
  193. # add shipping information
  194. # ship = lb[2].findAll('small')[1].findAll('span')[1].text.split("->")
  195. # shipFrom.append(ship[0].replace("Ship from ", "").strip())
  196. # shipTo.append(ship[1].replace("to ", "").strip())
  197. # Searching for CVE and MS categories
  198. cve = a.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  199. if not cve:
  200. cveValue = "-1"
  201. else:
  202. cee = " "
  203. for idx in cve:
  204. cee += (idx)
  205. cee += " "
  206. cee = cee.replace(',', ' ')
  207. cee = cee.replace('\n', '')
  208. cveValue = cee
  209. CVE.append(cveValue)
  210. ms = a.findAll(text=re.compile('MS\d{2}-\d{3}'))
  211. if not ms:
  212. MSValue = "-1"
  213. else:
  214. me = " "
  215. for im in ms:
  216. me += (im)
  217. me += " "
  218. me = me.replace(',', ' ')
  219. me = me.replace('\n', '')
  220. MSValue = me
  221. MS.append(MSValue)
  222. # Populate the final variable (this should be a list with all fields scraped)
  223. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  224. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href, image, image_vendor)
  225. # called by the crawler to get description links on a listing page
  226. # @param: beautifulsoup object that is using the correct html page (listing page)
  227. # return: list of description links from a listing page
  228. def gofish_links_parser(soup):
  229. # Returning all links that should be visited by the Crawler
  230. href = []
  231. listing = soup.find('tbody', {'class': 'border border-2 align-middle'})
  232. listing = soup.findAll('tr')
  233. listing = listing[1:]
  234. # for a in listing:
  235. # bae = a.find('a', {"class": "text-info"}, href=True)
  236. # link = bae['href']
  237. # href.append(link)
  238. for a in listing:
  239. bae = a.findAll('a', href=True)
  240. # Adding the url to the list of urls
  241. link = bae[0].get('href')
  242. href.append(link)
  243. return href