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.

253 lines
9.5 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').get('src')
  89. image = image.split('base64,')[-1]
  90. # Populating the final variable (this should be a list with all fields scraped)
  91. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  92. BTC, USD, EURO, sold, left, shipFrom, shipTo, image, vendor_image)
  93. # Sending the results
  94. return row
  95. #parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  96. #stores info it needs in different lists, these lists are returned after being organized
  97. #@param: soup object looking at html page of listing page
  98. #return: 'row' that contains a variety of lists that each hold info on the listing page
  99. def darkmatter_listing_parser(soup):
  100. # Fields to be parsed
  101. nm = 0 # *Total_Products (Should be Integer)
  102. mktName = "DarkMatter" # 0 *Marketplace_Name
  103. vendor = [] # 1 *Vendor y
  104. rating = [] # 2 Vendor_Rating
  105. success = [] # 3 Vendor_Successful_Transactions
  106. name = [] # 4 *Product_Name y
  107. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  108. MS = [] # 6 Product_MS_Classification (Microsoft Security)
  109. category = [] # 7 Product_Category y
  110. describe = [] # 8 Product_Description
  111. views = [] # 9 Product_Number_Of_Views
  112. reviews = [] # 10 Product_Number_Of_Reviews
  113. rating_item = [] # 11 Product_Rating
  114. addDate = [] # 12 Product_AddDate
  115. BTC = [] # 13 Product_BTC_SellingPrice
  116. USD = [] # 14 Product_USD_SellingPrice y
  117. EURO = [] # 15 Product_EURO_SellingPrice
  118. sold = [] # 16 Product_QuantitySold
  119. qLeft =[] # 17 Product_QuantityLeft
  120. shipFrom = [] # 18 Product_ShippedFrom
  121. shipTo = [] # 19 Product_ShippedTo
  122. image = [] # 20 Product_Image
  123. image_vendor = [] # 21 Vendor_Image
  124. href = [] # 22 Product_Links
  125. names = soup.find('div', {"class": "content"}).findAll('td', {"class": "lefted", "colspan": "3"})
  126. left = soup.find('div', {"class": "content"}).findAll('table', {"class": "vtable"})
  127. right = soup.find('div', {"class": "content"}).findAll('td', {"class": "vtop centered"})
  128. images = soup.find('div', {"class": "content"}).findAll('td', {"class": "vcentered"})
  129. # vtop centered
  130. count = 0
  131. # Populating the Number of Products
  132. nm = len(names)
  133. for a in names:
  134. # product name
  135. temp = a.find('a').text
  136. if ("pcs x " in temp):
  137. index = temp.index("pcs x ")
  138. result = temp[index + len("pcs x "):]
  139. name.append(cleanString(result))
  140. elif("pks x " in temp):
  141. index = temp.index("pks x ")
  142. result = temp[index + len("pks x "):]
  143. name.append(cleanString(result))
  144. CVE.append("-1")
  145. MS.append("-1")
  146. temp2 = left[count].findAll('tr')
  147. length_2 = len(temp2) - 1
  148. # category
  149. temp = temp2[1].find('td').text
  150. category.append(cleanString(temp.strip()))
  151. describe.append("-1")
  152. #escrow.append("-1")
  153. views.append("-1")
  154. reviews.append("-1")
  155. addDate.append("-1")
  156. #lastSeen.append("-1")
  157. BTC.append("-1")
  158. image_vendor.append("-1")
  159. # usd
  160. temp3 = right[count*2].find('span').text
  161. temp = temp3.replace(' USD', '')
  162. USD.append(cleanString(temp))
  163. EURO.append("-1")
  164. # 14 Product_QuantitySold
  165. temp3 = temp2[length_2].find('th').text
  166. temp3 = cleanString(temp3)
  167. if (temp3 == "Sold:"):
  168. temp = temp2[length_2].find('td').text
  169. sold.append(cleanString(temp.strip()))
  170. else:
  171. sold.append("-1")
  172. qLeft.append("-1")
  173. shipFrom.append("-1")
  174. # ship to
  175. temp3 = temp2[length_2].find('th').text
  176. temp3 = cleanString(temp3)
  177. if (temp3 == "Ship To:"):
  178. temp = temp2[length_2].find('td').text
  179. shipTo.append(cleanString(temp.strip()))
  180. else:
  181. shipTo.append("-1")
  182. # vendor
  183. temp = temp2[0].find('a').text
  184. vendor.append(cleanString(temp.strip()))
  185. # add product rating (stars)
  186. rating.append("-1")
  187. success.append("-1")
  188. temp = a.find('a').get('href')
  189. href.append(temp)
  190. # Finding Product Image
  191. image = images[count*2].find('img').get('src')
  192. image = image.split('base64,')[-1]
  193. count += 1
  194. rating_item.append("-1")
  195. # Populate the final variable (this should be a list with all fields scraped)
  196. return organizeProducts(mktName, nm, vendor, rating, success, name, CVE, MS, category, describe, views,
  197. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href, image, image_vendor)
  198. #called by the crawler to get description links on a listing page
  199. #@param: beautifulsoup object that is using the correct html page (listing page)
  200. #return: list of description links from a listing page
  201. def darkmatter_links_parser(soup):
  202. # Returning all links that should be visited by the Crawler
  203. href = []
  204. listing = soup.find('div', {"class": "content"}).findAll('td', {"class": "lefted", 'colspan': '3'})
  205. for a in listing:
  206. bae = a.find('a', href=True)
  207. link = bae['href']
  208. href.append(link)
  209. return href