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.

275 lines
10 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. # This is the method to parse the Description Pages (one page to each Product in the Listing Pages)
  7. def silkroad4_description_parser(soup):
  8. # Fields to be parsed
  9. vendor = "-1" # 0 *Vendor_Name
  10. success = "-1" # 1 Vendor_Successful_Transactions
  11. rating_vendor = "-1" # 2 Vendor_Rating
  12. name = "-1" # 3 *Product_Name
  13. describe = "-1" # 4 Product_Description
  14. CVE = "-1" # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  15. MS = "-1" # 6 Product_MS_Classification (Microsoft Security)
  16. category = "-1" # 7 Product_Category
  17. views = "-1" # 8 Product_Number_Of_Views
  18. reviews = "-1" # 9 Product_Number_Of_Reviews
  19. rating_item = "-1" # 10 Product_Rating
  20. addDate = "-1" # 11 Product_AddedDate
  21. BTC = "-1" # 12 Product_BTC_SellingPrice
  22. USD = "-1" # 13 Product_USD_SellingPrice
  23. EURO = "-1" # 14 Product_EURO_SellingPrice
  24. sold = "-1" # 15 Product_QuantitySold
  25. left = "-1" # 16 Product_QuantityLeft
  26. shipFrom = "-1" # 17 Product_ShippedFrom
  27. shipTo = "-1" # 18 Product_ShippedTo
  28. LTC = "-1" # 19 Product_LTC_SellingPrice
  29. XMR = "-1" # 20 Product_XMR_SellingPrice
  30. image = "-1" # 19 Product_Image
  31. vendor_image = "-1" # 20 Vendor_Image
  32. bae = soup.find('div', {'id': 'cats'})
  33. # Finding Vendor
  34. vendor = bae.find('font', {'color':'blue'}).text
  35. vendor = vendor.strip()
  36. #Finding Product Name
  37. name = bae.find('b', {'style': 'color:#333;'}).text
  38. name = name.strip()
  39. #Finding image
  40. image = bae.find('img')
  41. image = image.get('src')
  42. image = image.split('base64,')[-1]
  43. #Finding Price
  44. temp = bae.find('span').next_sibling
  45. price_list = temp.split("/")
  46. USD = price_list[0].replace("$", "").strip()
  47. BTC = price_list[1].replace("BTC","").strip()
  48. # Not stored into databse/PGAdmin yet!
  49. LTC = price_list[2].replace("LTC", "").strip()
  50. XMR = price_list[3].replace("XMR", "").strip()
  51. #print(USD, "", BTC,"", LTC,"", XMR)
  52. # Finding Ships From
  53. a = bae.find_all('a',href=True, limit = 2)
  54. shipFrom = a[0].text.strip()
  55. #Finding Category
  56. category = a[1].text.strip()
  57. # Finding the Product description
  58. describe = soup.find('div', {'style': 'color:#555;font-weight:normal;font-size:12px'}).text
  59. describe = cleanString(describe.strip())
  60. # Finding Rating
  61. rate = soup.find('div', {'style': 'padding:0px; margin-bottom:10px; font-size:12px;'})
  62. rate = rate.find('p')
  63. if rate is not None:
  64. rate = rate.text.strip()
  65. #Some descriptions have rating as 'No Rating yet', can convert to -1 for consistency in database
  66. #if(rate is 'No rating yet'):
  67. # rating_item = -1
  68. #Only extract rating part
  69. rating_item = rate[:rate.index('Note')]
  70. # Finding Number of Reviews
  71. table = soup.find('div', {'class': 'table-responsive'})
  72. if table is not None:
  73. num_rev = table.findAll('tr')
  74. reviews = len(num_rev) - 1
  75. # Searching for CVE and MS categories
  76. cve = soup.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  77. if cve:
  78. CVE = " "
  79. for idx in cve:
  80. CVE += (idx)
  81. CVE += " "
  82. CVE = CVE.replace(',', ' ')
  83. CVE = CVE.replace('\n', '')
  84. ms = soup.findAll(text=re.compile('MS\d{2}-\d{3}'))
  85. if ms:
  86. MS = " "
  87. for im in ms:
  88. MS += (im)
  89. MS += " "
  90. MS = MS.replace(',', ' ')
  91. MS = MS.replace('\n', '')
  92. # Populating the final variable (this should be a list with all fields scraped)
  93. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  94. BTC, USD, EURO, sold, left, shipFrom, shipTo, image, vendor_image)
  95. # Sending the results
  96. return row
  97. # This is the method to parse the Listing Pages
  98. def silkroad4_listing_parser(soup):
  99. # Fields to be parsed
  100. nm = 0 # *Total_Products (Should be Integer)
  101. mktName = "SilkRoad4" # 0 *Marketplace_Name
  102. vendor = [] # 1 *Vendor y
  103. rating_vendor = [] # 2 Vendor_Rating
  104. success = [] # 3 Vendor_Successful_Transactions
  105. name = [] # 4 *Product_Name y
  106. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  107. MS = [] # 6 Product_MS_Classification (Microsoft Security)
  108. category = [] # 7 Product_Category y
  109. describe = [] # 8 Product_Description
  110. views = [] # 9 Product_Number_Of_Views
  111. reviews = [] # 10 Product_Number_Of_Reviews
  112. rating_item = [] # 11 Product_Rating
  113. addDate = [] # 12 Product_AddDate
  114. BTC = [] # 13 Product_BTC_SellingPrice
  115. USD = [] # 14 Product_USD_SellingPrice y
  116. EURO = [] # 15 Product_EURO_SellingPrice
  117. sold = [] # 16 Product_QuantitySold
  118. qLeft =[] # 17 Product_QuantityLeft
  119. shipFrom = [] # 18 Product_ShippedFrom
  120. shipTo = [] # 19 Product_ShippedTo
  121. href = [] # 20 Product_Links
  122. LTC = [] # 21 Product_LTC_SellingPrice
  123. XMR = [] # 22 Product_XMR_SellingPrice
  124. image = [] # 19 Product_Image
  125. image_vendor = [] # 20 Vendor_Image
  126. listing = soup.findAll('div', {'style': "padding:10px; width:100%; margin-bottom:5px; margin-top:0px"})
  127. # Populating the Number of Products
  128. nm = len(listing)
  129. # Finding category of listing page
  130. cat = listing[0].find_all('a', href=True)
  131. cat = cat[len(cat)-2].text
  132. cat = cat.replace(",", "")
  133. cat = cat.strip()
  134. for a in listing:
  135. bae = a.findAll('a', href=True)
  136. # Adding the category
  137. category.append(cat)
  138. # Adding the url to the list of urls
  139. link = bae[0].get('href')
  140. link = cleanLink(link)
  141. href.append(link)
  142. # Finding Price
  143. temp = a.find('b', {'style': 'color:#333'})
  144. temp = temp.text
  145. price = temp.split("/")
  146. USD.append(price[0].replace('$', '').strip())
  147. BTC.append(price[1].replace('BTC', '').strip())
  148. # LTC and XMR will not be stored in Pgadmin as of now
  149. LTC.append(price[2].replace('LTC', '').strip())
  150. XMR.append(price[3].replace('XMR', '').strip())
  151. #print(USD, " ", BTC, ' ', LTC, '', XMR)
  152. # Finding the Vendor
  153. ven = a.findAll('b')
  154. v = ven[2].find('font').text
  155. v = v.strip()
  156. vendor.append(v)
  157. # Finding the Product
  158. product = bae[0].text
  159. product = product.replace(",", "")
  160. product = product.replace(",", "")
  161. product = product.replace("...", "")
  162. product = product.strip()
  163. name.append(product)
  164. # Finding ShipFrom
  165. shipf = bae[len(bae)-1].text.strip()
  166. shipFrom.append(shipf)
  167. # Finding image
  168. product_image = a.find('img')
  169. product_image = product_image.get('src')
  170. product_image = product_image.split('base64,')[-1]
  171. image.append(product_image)
  172. # Searching for CVE and MS categories
  173. cve = a.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  174. if not cve:
  175. cveValue="-1"
  176. else:
  177. cee = " "
  178. for idx in cve:
  179. cee += (idx)
  180. cee += " "
  181. cee = cee.replace(',', ' ')
  182. cee = cee.replace('\n', '')
  183. cveValue=cee
  184. CVE.append(cveValue)
  185. ms = a.findAll(text=re.compile('MS\d{2}-\d{3}'))
  186. if not ms:
  187. MSValue="-1"
  188. else:
  189. me = " "
  190. for im in ms:
  191. me += (im)
  192. me += " "
  193. me = me.replace(',', ' ')
  194. me = me.replace('\n', '')
  195. MSValue=me
  196. MS.append(MSValue)
  197. # Populate the final variable (this should be a list with all fields scraped)
  198. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  199. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href, image,
  200. image_vendor)
  201. def silkroad4_links_parser(soup):
  202. # Returning all links that should be visited by the Crawler
  203. href = []
  204. # finds all div with id, vp
  205. divs = soup.findAll('div', {"id": "vp"})
  206. listing = []
  207. # for all div with id:vp, find first a with href
  208. for div in divs:
  209. a_s = div.find('a', href=True)
  210. # if div has no href True, then move on. otherwise, store the a with href into listing
  211. if a_s is not None:
  212. listing.append(a_s)
  213. # loop through listing with a with href and extract/return the href
  214. for a in listing:
  215. link = a['href']
  216. # if '?listing' is in link, then it is a product, so append to the href list. Otherwise, move on.
  217. if "?listing" in link:
  218. href.append(link)
  219. return href