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.

322 lines
12 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  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 darkmatter_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. # 0 *Vendor_Name
  34. try:
  35. temp = soup.find('table', {'class', 'vtable'})
  36. temp = temp.findAll('tr')
  37. temp2 = temp[3].find('a').text
  38. name = cleanString(temp2.strip())
  39. except:
  40. try:
  41. temp = soup.find('table', {'class', 'vtable'})
  42. temp = temp.findAll('tr')
  43. temp2 = temp[4].find('a').text
  44. name = cleanString(temp2.strip())
  45. except:
  46. print("vendor")
  47. # product name
  48. try:
  49. name = soup.find('div', {'class', 'title-h2'}).text
  50. name = cleanString(name.strip())
  51. except:
  52. print("name")
  53. #product description
  54. try:
  55. temp = soup.find('pre', {'class', 'description'}).text
  56. temp = temp.replace('\n', ' ')
  57. describe = cleanString(temp.strip())
  58. except:
  59. print("description")
  60. # Finding Product Image
  61. #image = soup.find('div', {'class': 'woocommerce-product-gallery__image'}).find('img')
  62. #image = image.get('src')
  63. #product category
  64. try:
  65. temp = soup.find('table', {'class', 'vtable'})
  66. temp = temp.findAll('tr')
  67. temp2 = temp[4].find('th').text
  68. temp2 = cleanString(temp2)
  69. if (temp2 == "Category"):
  70. temp2 = temp[4].find('a').text
  71. category = cleanString(temp2.strip())
  72. except:
  73. try:
  74. temp = soup.find('table', {'class', 'vtable'})
  75. temp = temp.findAll('tr')
  76. temp2 = temp[5].find('th').text
  77. temp2 = cleanString(temp2.strip)
  78. if (temp2 == "Category"):
  79. temp2 = temp[5].find('a').text
  80. category = cleanString(temp2.strip())
  81. except:
  82. print('category')
  83. # usd
  84. try:
  85. temp = soup.find('table', {'class', 'vtable'})
  86. temp = temp.findAll('tr')
  87. temp2 = temp[1].find('td').text
  88. temp2 = temp2.replace(' USD', '')
  89. USD = cleanString(temp2)
  90. except:
  91. print('USD')
  92. # 15 Product_QuantitySold
  93. try:
  94. temp = soup.find('table', {'class', 'vtable'})
  95. temp = temp.findAll('tr')
  96. temp2 = temp[5].find('th').text
  97. temp2 = cleanString(temp2)
  98. temp3 = temp[6].find('th').text
  99. temp3 = cleanString(temp3)
  100. if (temp2 == "Sold"):
  101. temp2 = temp[5].find('td').text
  102. sold = cleanString(temp2.strip())
  103. elif (temp3 == "Sold"):
  104. temp2 = temp[6].find('td').text
  105. sold = cleanString(temp2.strip())
  106. except:
  107. print('sold')
  108. # Populating the final variable (this should be a list with all fields scraped)
  109. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  110. BTC, USD, EURO, sold, left, shipFrom, shipTo, image, vendor_image)
  111. # Sending the results
  112. return row
  113. #parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  114. #stores info it needs in different lists, these lists are returned after being organized
  115. #@param: soup object looking at html page of listing page
  116. #return: 'row' that contains a variety of lists that each hold info on the listing page
  117. def darkmatter_listing_parser(soup):
  118. """
  119. # Fields to be parsed
  120. nm = 0 # Total_Products (Should be Integer)
  121. mktName = "DarkMatter" # 0 Marketplace_Name
  122. name = [] # 1 Product_Name
  123. CVE = [] # 2 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  124. MS = [] # 3 Product_MS_Classification (Microsoft Security)
  125. category = [] # 4 Product_Category
  126. describe = [] # 5 Product_Description
  127. escrow = [] # 6 Vendor_Warranty
  128. views = [] # 7 Product_Number_Of_Views
  129. reviews = [] # 8 Product_Number_Of_Reviews
  130. addDate = [] # 9 Product_AddDate
  131. rating_item = [] # 11 Product_Rating
  132. lastSeen = [] # 10 Product_LastViewDate
  133. BTC = [] # 11 Product_BTC_SellingPrice
  134. USD = [] # 12 Product_USD_SellingPrice
  135. EURO = [] # 13 Product_EURO_SellingPrice
  136. sold = [] # 14 Product_QuantitySold
  137. qLeft =[] # 15 Product_QuantityLeft
  138. shipFrom = [] # 16 Product_ShippedFrom
  139. shipTo = [] # 17 Product_ShippedTo
  140. vendor = [] # 18 Vendor
  141. rating = [] # 19 Vendor_Rating
  142. success = [] # 20 Vendor_Successful_Transactions
  143. href = [] # 23 Product_Links (Urls)
  144. """
  145. # Fields to be parsed
  146. nm = 0 # *Total_Products (Should be Integer)
  147. mktName = "DarkMatter" # 0 *Marketplace_Name
  148. vendor = [] # 1 *Vendor y
  149. rating = [] # 2 Vendor_Rating
  150. success = [] # 3 Vendor_Successful_Transactions
  151. name = [] # 4 *Product_Name y
  152. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  153. MS = [] # 6 Product_MS_Classification (Microsoft Security)
  154. category = [] # 7 Product_Category y
  155. describe = [] # 8 Product_Description
  156. views = [] # 9 Product_Number_Of_Views
  157. reviews = [] # 10 Product_Number_Of_Reviews
  158. rating_item = [] # 11 Product_Rating
  159. addDate = [] # 12 Product_AddDate
  160. BTC = [] # 13 Product_BTC_SellingPrice
  161. USD = [] # 14 Product_USD_SellingPrice y
  162. EURO = [] # 15 Product_EURO_SellingPrice
  163. sold = [] # 16 Product_QuantitySold
  164. qLeft =[] # 17 Product_QuantityLeft
  165. shipFrom = [] # 18 Product_ShippedFrom
  166. shipTo = [] # 19 Product_ShippedTo
  167. image = [] # 20 Product_Image
  168. image_vendor = [] # 21 Vendor_Image
  169. href = [] # 22 Product_Links
  170. names = soup.find('div', {"class": "content"}).findAll('td', {"class": "lefted", "colspan": "3"})
  171. left = soup.find('div', {"class": "content"}).findAll('table', {"class": "vtable"})
  172. right = soup.find('div', {"class": "content"}).findAll('td', {"class": "vtop centered"})
  173. # vtop centered
  174. count = 0
  175. # Populating the Number of Products
  176. nm = len(names)
  177. for a in names:
  178. # product name
  179. try:
  180. temp = a.find('a').text
  181. if ("pcs x " in temp):
  182. index = temp.index("pcs x ")
  183. result = temp[index + len("pcs x "):]
  184. name.append(cleanString(result))
  185. elif("pks x " in temp):
  186. index = temp.index("pks x ")
  187. result = temp[index + len("pks x "):]
  188. name.append(cleanString(temp))
  189. except Exception as e:
  190. print("product name", e)
  191. # Finding Product Image
  192. #product_image = a.find('img', {'class': 'attachment-woocommerce_thumbnail size-woocommerce_thumbnail'})
  193. #product_image = product_image.get('src')
  194. #image.append(product_image)
  195. CVE.append("-1")
  196. MS.append("-1")
  197. temp2 = left[count].findAll('tr')
  198. length_2 = len(temp2) - 1
  199. # category
  200. try:
  201. temp = temp2[1].find('td').text
  202. category.append(cleanString(temp.strip()))
  203. except:
  204. print('category')
  205. describe.append("-1")
  206. #escrow.append("-1")
  207. views.append("-1")
  208. reviews.append("-1")
  209. addDate.append("-1")
  210. #lastSeen.append("-1")
  211. BTC.append("-1")
  212. image.append("-1")
  213. image_vendor.append("-1")
  214. # usd
  215. try:
  216. temp3 = right[count*2].find('span').text
  217. temp = temp3.replace(' USD', '')
  218. USD.append(cleanString(temp))
  219. except:
  220. print('USD')
  221. EURO.append("-1")
  222. # 14 Product_QuantitySold
  223. try:
  224. temp3 = temp2[length_2].find('th').text
  225. temp3 = cleanString(temp3)
  226. if (temp3 == "Sold:"):
  227. temp = temp2[length_2].find('td').text
  228. sold.append(cleanString(temp.strip()))
  229. else:
  230. sold.append("-1")
  231. except Exception as e:
  232. sold.append("-1")
  233. print('sold', e)
  234. qLeft.append("-1")
  235. shipFrom.append("-1")
  236. # ship to
  237. try:
  238. temp3 = temp2[length_2].find('th').text
  239. temp3 = cleanString(temp3)
  240. if (temp3 == "Ship To:"):
  241. temp = temp2[length_2].find('td').text
  242. shipTo.append(cleanString(temp.strip()))
  243. else:
  244. shipTo.append("-1")
  245. except Exception as e:
  246. shipTo.append("-1")
  247. print('shopto')
  248. # vendor
  249. try:
  250. temp = temp2[0].find('a').text
  251. vendor.append(cleanString(temp.strip()))
  252. except:
  253. print('vendor')
  254. rating.append("-1")
  255. success.append("-1")
  256. try:
  257. temp = a.find('a').get('href')
  258. href.append(temp)
  259. except:
  260. print('href')
  261. count += 1
  262. rating_item.append("-1")
  263. # Populate the final variable (this should be a list with all fields scraped)
  264. return organizeProducts(mktName, nm, vendor, rating, success, name, CVE, MS, category, describe, views,
  265. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href, image, image_vendor)
  266. #called by the crawler to get description links on a listing page
  267. #@param: beautifulsoup object that is using the correct html page (listing page)
  268. #return: list of description links from a listing page
  269. def darkmatter_links_parser(soup):
  270. # Returning all links that should be visited by the Crawler
  271. href = []
  272. listing = soup.find('div', {"class": "content"}).findAll('td', {"class": "lefted", 'colspan': '3'})
  273. for a in listing:
  274. bae = a.find('a', href=True)
  275. link = bae['href']
  276. href.append(link)
  277. return href