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.

260 lines
9.7 KiB

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. vendor = cleanString(temp2.strip())
  39. except:
  40. temp = soup.find('table', {'class', 'vtable'})
  41. temp = temp.findAll('tr')
  42. temp2 = temp[4].find('a').text
  43. vendor = cleanString(temp2.strip())
  44. # product name
  45. name = soup.find('div', {'class', 'title-h2'}).text
  46. name = cleanString(name.strip())
  47. #product description
  48. temp = soup.find('pre', {'class', 'description'}).text
  49. temp = temp.replace('\n', ' ')
  50. describe = cleanString(temp.strip())
  51. #product category
  52. try:
  53. temp = soup.find('table', {'class', 'vtable'})
  54. temp = temp.findAll('tr')
  55. temp2 = temp[4].find('th').text
  56. temp2 = cleanString(temp2)
  57. if (temp2 == "Category"):
  58. temp2 = temp[4].find('a').text
  59. category = cleanString(temp2.strip())
  60. except:
  61. temp = soup.find('table', {'class', 'vtable'})
  62. temp = temp.findAll('tr')
  63. temp2 = temp[5].find('th').text
  64. temp2 = cleanString(temp2.strip)
  65. if (temp2 == "Category"):
  66. temp2 = temp[5].find('a').text
  67. category = cleanString(temp2.strip())
  68. # usd
  69. temp = soup.find('table', {'class', 'vtable'})
  70. temp = temp.findAll('tr')
  71. temp2 = temp[1].find('td').text
  72. temp2 = temp2.replace(' USD', '')
  73. USD = cleanString(temp2)
  74. # 15 Product_QuantitySold
  75. temp = soup.find('table', {'class', 'vtable'})
  76. temp = temp.findAll('tr')
  77. temp2 = temp[5].find('th').text
  78. temp2 = cleanString(temp2)
  79. temp3 = temp[6].find('th').text
  80. temp3 = cleanString(temp3)
  81. if (temp2 == "Sold"):
  82. temp2 = temp[5].find('td').text
  83. sold = cleanString(temp2.strip())
  84. elif (temp3 == "Sold"):
  85. temp2 = temp[6].find('td').text
  86. sold = cleanString(temp2.strip())
  87. # Finding Product Image
  88. image = soup.find('td', {"class": "vtop"}).find('img')
  89. if image is not None:
  90. image = image.get('src').split('base64,')[-1]
  91. else:
  92. image = '-1'
  93. # Populating the final variable (this should be a list with all fields scraped)
  94. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  95. BTC, USD, EURO, sold, left, shipFrom, shipTo, image, vendor_image)
  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 darkmatter_listing_parser(soup):
  103. # Fields to be parsed
  104. nm = 0 # *Total_Products (Should be Integer)
  105. mktName = "DarkMatter" # 0 *Marketplace_Name
  106. vendor = [] # 1 *Vendor y
  107. rating = [] # 2 Vendor_Rating
  108. success = [] # 3 Vendor_Successful_Transactions
  109. name = [] # 4 *Product_Name y
  110. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  111. MS = [] # 6 Product_MS_Classification (Microsoft Security)
  112. category = [] # 7 Product_Category y
  113. describe = [] # 8 Product_Description
  114. views = [] # 9 Product_Number_Of_Views
  115. reviews = [] # 10 Product_Number_Of_Reviews
  116. rating_item = [] # 11 Product_Rating
  117. addDate = [] # 12 Product_AddDate
  118. BTC = [] # 13 Product_BTC_SellingPrice
  119. USD = [] # 14 Product_USD_SellingPrice y
  120. EURO = [] # 15 Product_EURO_SellingPrice
  121. sold = [] # 16 Product_QuantitySold
  122. qLeft =[] # 17 Product_QuantityLeft
  123. shipFrom = [] # 18 Product_ShippedFrom
  124. shipTo = [] # 19 Product_ShippedTo
  125. image = [] # 20 Product_Image
  126. image_vendor = [] # 21 Vendor_Image
  127. href = [] # 22 Product_Links
  128. names = soup.find('div', {"class": "content"}).findAll('td', {"class": "lefted", "colspan": "3"})
  129. left = soup.find('div', {"class": "content"}).findAll('table', {"class": "vtable"})
  130. right = soup.find('div', {"class": "content"}).findAll('td', {"class": "vtop centered"})
  131. images = soup.find('div', {"class": "content"}).findAll('td', {"class": "vcentered"})
  132. # vtop centered
  133. count = 0
  134. # Populating the Number of Products
  135. nm = len(names)
  136. for a in names:
  137. # product name
  138. temp = a.find('a').text
  139. if ("pcs x " in temp):
  140. index = temp.index("pcs x ")
  141. result = temp[index + len("pcs x "):]
  142. name.append(cleanString(result))
  143. elif("pks x " in temp):
  144. index = temp.index("pks x ")
  145. result = temp[index + len("pks x "):]
  146. name.append(cleanString(result))
  147. elif ("job x " in temp):
  148. index = temp.index("job x ")
  149. result = temp[index + len("job x "):]
  150. name.append(cleanString(result))
  151. CVE.append("-1")
  152. MS.append("-1")
  153. temp2 = left[count].findAll('tr')
  154. length_2 = len(temp2) - 1
  155. # category
  156. temp = temp2[1].find('td').text
  157. category.append(cleanString(temp.strip()))
  158. describe.append("-1")
  159. #escrow.append("-1")
  160. views.append("-1")
  161. reviews.append("-1")
  162. addDate.append("-1")
  163. #lastSeen.append("-1")
  164. BTC.append("-1")
  165. image_vendor.append("-1")
  166. # usd
  167. temp3 = right[count*2].find('span').text
  168. temp = temp3.replace(' USD', '')
  169. USD.append(cleanString(temp))
  170. EURO.append("-1")
  171. # 14 Product_QuantitySold
  172. temp3 = temp2[length_2].find('th').text
  173. temp3 = cleanString(temp3)
  174. if (temp3 == "Sold:"):
  175. temp = temp2[length_2].find('td').text
  176. sold.append(cleanString(temp.strip()))
  177. else:
  178. sold.append("-1")
  179. qLeft.append("-1")
  180. shipFrom.append("-1")
  181. # ship to
  182. temp3 = temp2[length_2].find('th').text
  183. temp3 = cleanString(temp3)
  184. if (temp3 == "Ship To:"):
  185. temp = temp2[length_2].find('td').text
  186. shipTo.append(cleanString(temp.strip()))
  187. else:
  188. shipTo.append("-1")
  189. # vendor
  190. temp = temp2[0].find('a').text
  191. vendor.append(cleanString(temp.strip()))
  192. # add product rating (stars)
  193. rating.append("-1")
  194. success.append("-1")
  195. temp = a.find('a').get('href')
  196. href.append(temp)
  197. # Finding Product Image
  198. image = images[count*2].find('img').get('src')
  199. image = image.split('base64,')[-1]
  200. count += 1
  201. rating_item.append("-1")
  202. # Populate the final variable (this should be a list with all fields scraped)
  203. return organizeProducts(mktName, nm, vendor, rating, success, name, CVE, MS, category, describe, views,
  204. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href, image, image_vendor)
  205. #called by the crawler to get description links on a listing page
  206. #@param: beautifulsoup object that is using the correct html page (listing page)
  207. #return: list of description links from a listing page
  208. def darkmatter_links_parser(soup):
  209. # Returning all links that should be visited by the Crawler
  210. href = []
  211. listing = soup.find('div', {"class": "content"}).findAll('td', {"class": "lefted", 'colspan': '3'})
  212. for a in listing:
  213. bae = a.find('a', href=True)
  214. link = bae['href']
  215. href.append(link)
  216. return href