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.

282 lines
9.8 KiB

  1. __author__ = 'DarkWeb'
  2. import re
  3. # Here, we are importing the auxiliary functions to clean or convert data
  4. from MarketPlaces.Utilities.utilities import *
  5. # Here, we are importing BeautifulSoup to search through the HTML tree
  6. from bs4 import BeautifulSoup
  7. # This is the method to parse the Description Pages (one page to each Product in the Listing Pages)
  8. def vicecity_description_parser(soup):
  9. # Fields to be parsed
  10. vendor = "-1" # 0 *Vendor_Name
  11. success = "-1" # 1 Vendor_Successful_Transactions
  12. rating_vendor = "-1" # 2 Vendor_Rating
  13. name = "-1" # 3 *Product_Name
  14. describe = "-1" # 4 Product_Description
  15. CVE = "-1" # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  16. MS = "-1" # 6 Product_MS_Classification (Microsoft Security)
  17. category = "-1" # 7 Product_Category
  18. views = "-1" # 8 Product_Number_Of_Views
  19. reviews = "-1" # 9 Product_Number_Of_Reviews
  20. rating_item = "-1" # 10 Product_Rating
  21. addDate = "-1" # 11 Product_AddedDate
  22. BTC = "-1" # 12 Product_BTC_SellingPrice
  23. USD = "-1" # 13 Product_USD_SellingPrice
  24. EURO = "-1" # 14 Product_EURO_SellingPrice
  25. sold = "-1" # 15 Product_QuantitySold
  26. left = "-1" # 16 Product_QuantityLeft
  27. shipFrom = "-1" # 17 Product_ShippedFrom
  28. shipTo = "-1" # 18 Product_ShippedTo
  29. # Finding Product Name
  30. name = soup.find('div', {'class': "listing_info"}).find('div', {'class': "listing_right"})
  31. name = name.find('span', {'style': "font-size:18px;font-weight: bold;color: #fff"}).text
  32. name = name.replace('\n', ' ')
  33. name = name.replace(",", "")
  34. name = name.strip()
  35. # Finding Vendor
  36. vendor = soup.find('div', {'class': "listing_info"})
  37. vendor = vendor.find('div', {'class': "listing_right"})
  38. numbers = vendor.find('a').find('span').text
  39. vendor = vendor.find('a').text
  40. vendor = vendor.replace(numbers, "").strip() # removes numbers at the end of vendor name
  41. # Finding Vendor Rating
  42. rating = soup.find('div', {'class': "listing_info"}).find('div', {'class': "listing_right"}).find('a').get('title')
  43. rating = re.search(r"\d+%", rating)
  44. rating_vendor = rating.group(0).strip()
  45. # Finding Quantity Sold and Left
  46. # temp = mb[4].text.split(',')
  47. #
  48. # sold = temp[0].replace("sold", "")
  49. # sold = sold.strip()
  50. #
  51. # left = temp[1].replace("in stock", "")
  52. # left = left.strip()
  53. # Finding Successful Transactions
  54. success = soup.find('div', {'class': "listing_info"}).find('div', {'class': "listing_right"}).find('a').get('title')
  55. success = re.search(r"\d+(?= sales)", success)
  56. success = success.group(0).strip()
  57. bae = soup.find('pre')
  58. # Finding USD
  59. USD = bae.find('span').text
  60. USD = re.search(r"\$\d+(?:\.\d+)?", USD).group(0)
  61. USD = USD.replace("$", "").strip()
  62. # Finding BTC
  63. BTC = bae.find_all('span')
  64. BTC = re.search(r"\d+(?:\.\d+)?", BTC[1].text).group(0)
  65. BTC = BTC.strip()
  66. # Finding the Product Category
  67. category = soup.find('div', {'class': "listing_info"}).find('div', {'class': "listing_right"})
  68. category = category.find('span', {'style': "font-size:15px;color: #a1a1a1"}).text
  69. category = category.replace("Category:", "").strip()
  70. li = bae.find_all('span')
  71. # Finding Shipment Information (Origin)
  72. shipFrom = li[-4].text.strip()
  73. # Finding Shipment Information (Destination)
  74. shipTo = li[-2].text.strip()
  75. # Finding the Product description
  76. describe = soup.find('p', {
  77. 'style': "width:705px;margin-left:-305px;background-color: #242424;border-radius: 3px;border: 1px solid #373737;padding: 5px;"}).text
  78. describe = describe.replace("\n", " ")
  79. describe = describe.strip()
  80. # Finding the Number of Product Reviews
  81. li = soup.find_all('label', {'class': "tc_label threetabs"})
  82. review = li[1].text
  83. review = re.search(r"\d+", review)
  84. if review:
  85. reviews = review.group(0).strip()
  86. else:
  87. reviews = '0'
  88. # Searching for CVE and MS categories
  89. cve = soup.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  90. if cve:
  91. CVE = " "
  92. for idx in cve:
  93. CVE += (idx)
  94. CVE += " "
  95. CVE = CVE.replace(',', ' ')
  96. CVE = CVE.replace('\n', '')
  97. ms = soup.findAll(text=re.compile('MS\d{2}-\d{3}'))
  98. if ms:
  99. MS = " "
  100. for im in ms:
  101. MS += (im)
  102. MS += " "
  103. MS = MS.replace(',', ' ')
  104. MS = MS.replace('\n', '')
  105. # Populating the final variable (this should be a list with all fields scraped)
  106. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  107. BTC, USD, EURO, sold, left, shipFrom, shipTo)
  108. # Sending the results
  109. return row
  110. # This is the method to parse the Listing Pages
  111. def vicecity_listing_parser(soup):
  112. # Fields to be parsed
  113. nm = 0 # *Total_Products (Should be Integer)
  114. mktName = "ViceCity" # 0 *Marketplace_Name
  115. vendor = [] # 1 *Vendor y
  116. rating_vendor = [] # 2 Vendor_Rating
  117. success = [] # 3 Vendor_Successful_Transactions
  118. name = [] # 4 *Product_Name y
  119. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  120. MS = [] # 6 Product_MS_Classification (Microsoft Security)
  121. category = [] # 7 Product_Category y
  122. describe = [] # 8 Product_Description
  123. views = [] # 9 Product_Number_Of_Views
  124. reviews = [] # 10 Product_Number_Of_Reviews
  125. rating_item = [] # 11 Product_Rating
  126. addDate = [] # 12 Product_AddDate
  127. BTC = [] # 13 Product_BTC_SellingPrice
  128. USD = [] # 14 Product_USD_SellingPrice y
  129. EURO = [] # 15 Product_EURO_SellingPrice
  130. sold = [] # 16 Product_QuantitySold
  131. qLeft = [] # 17 Product_QuantityLeft
  132. shipFrom = [] # 18 Product_ShippedFrom
  133. shipTo = [] # 19 Product_ShippedTo
  134. href = [] # 20 Product_Links
  135. listing = soup.findAll('div', {"class": "wLf"}) # should be 30
  136. # Populating the Number of Products
  137. nm = len(listing)
  138. # # Finding Category
  139. # cat = soup.find("div", {"class": "col-9"})
  140. # cat = cat.find("h2").text
  141. # cat = cat.replace("Category: ", "")
  142. # cat = cat.replace(",", "")
  143. # cat = cat.strip()
  144. for a in listing:
  145. # category.append(cat)
  146. # bae = card.findAll('a')
  147. # Adding the url to the list of urls
  148. link = a.find('div', {"class": "wLfLeft"}).find('a', href=True).get('href')
  149. link = cleanLink(link)
  150. href.append(link)
  151. # Finding the Product Name
  152. product = a.find('div', {"class": "wLfName"}).find('a').text
  153. product = product.replace('\n', ' ')
  154. product = product.replace(",", "")
  155. product = product.replace("...", "")
  156. product = product.strip()
  157. name.append(product)
  158. # Finding the Vendor
  159. vendor_name = a.find('div', {"class": "wLfVendor"}).find('a').text
  160. addedNums = a.find('div', {"class": "wLfVendor"}).find('a').find('span').text # finds numbers added at end
  161. vendor_name = vendor_name.replace(",", "")
  162. vendor_name = vendor_name.replace(addedNums, "") # removes numbers added at end
  163. vendor_name = vendor_name.strip()
  164. vendor.append(vendor_name)
  165. # Finding Prices
  166. price = a.find('div', {"class": "wLfPrice"}).find_all('span')
  167. ud = price[0].text.replace(" USD", " ")
  168. # u = ud.replace("$","")
  169. ud = ud.replace(",", "")
  170. u = ud.replace(price[1].text, "")
  171. u = u.strip()
  172. USD.append(u)
  173. bc = price[1].text
  174. bc = re.search(r"\d+(?:\.\d+)?", bc).group(0).strip()
  175. BTC.append(bc)
  176. # # Finding Reviews
  177. # num = card.find("span", {"class": "rate-count"}).text
  178. # num = num.replace("(", "")
  179. # num = num.replace("review)", "")
  180. # num = num.replace("reviews)", "")
  181. # num = num.strip()
  182. # reviews.append(num)
  183. # Finding Successful Transactions
  184. freq = a.find('div', {"class": "wLfVendor"}).find('a').get('title')
  185. freq = re.search(r'\d+(?= sales)', freq).group(0)
  186. freq = freq.strip()
  187. success.append(freq)
  188. # Finding Ship from and ship to
  189. place = a.find('div', {"class": "wLfPrice"})
  190. place = place.find('span', {'style': "font-size: 12px;"}).text
  191. place = place.split('')
  192. varFrom = place[0].strip()
  193. varTo = place[1].strip()
  194. if varFrom == "WW":
  195. varFrom = "Worldwide"
  196. if varTo == "WW":
  197. varTo = "Worldwide"
  198. shipFrom.append(varFrom)
  199. shipTo.append(varTo)
  200. # Searching for CVE and MS categories
  201. cve = a.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  202. if not cve:
  203. cveValue = "-1"
  204. else:
  205. cee = " "
  206. for idx in cve:
  207. cee += (idx)
  208. cee += " "
  209. cee = cee.replace(',', ' ')
  210. cee = cee.replace('\n', '')
  211. cveValue = cee
  212. CVE.append(cveValue)
  213. ms = a.findAll(text=re.compile('MS\d{2}-\d{3}'))
  214. if not ms:
  215. MSValue = "-1"
  216. else:
  217. me = " "
  218. for im in ms:
  219. me += (im)
  220. me += " "
  221. me = me.replace(',', ' ')
  222. me = me.replace('\n', '')
  223. MSValue = me
  224. MS.append(MSValue)
  225. # Populate the final variable (this should be a list with all fields scraped)
  226. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  227. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href)
  228. def vicecity_links_parser(soup):
  229. # Returning all links that should be visited by the Crawler
  230. href = []
  231. listing = soup.findAll('div', {"class": "wLf"})
  232. for a in listing:
  233. bae = a.find('div', {"class": "wLfLeft"}).find('a', href=True)
  234. link = bae['href']
  235. href.append(link)
  236. return href