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.

333 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 year ago
1 year ago
1 year ago
1 year ago
  1. __author__ = 'chris'
  2. import re
  3. import traceback
  4. # Here, we are importing the auxiliary functions to clean or convert data
  5. from MarketPlaces.Utilities.utilities import *
  6. # Here, we are importing BeautifulSoup to search through the HTML tree
  7. from bs4 import BeautifulSoup
  8. # Import for test run
  9. import glob
  10. import os
  11. import codecs
  12. import shutil
  13. # This is the method to parse the Description Pages (one page to each Product in the Listing Pages)
  14. def Robinhood_description_parser(soup):
  15. # Fields to be parsed
  16. vendor = "-1" # 0 *Vendor_Name
  17. success = "-1" # 1 Vendor_Successful_Transactions
  18. rating_vendor = "-1" # 2 Vendor_Rating
  19. name = "-1" # 3 *Product_Name
  20. describe = "-1" # 4 Product_Description
  21. CVE = "-1" # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  22. MS = "-1" # 6 Product_MS_Classification (Microsoft Security)
  23. category = "-1" # 7 Product_Category
  24. views = "-1" # 8 Product_Number_Of_Views
  25. reviews = "-1" # 9 Product_Number_Of_Reviews
  26. rating_item = "-1" # 10 Product_Rating
  27. addDate = "-1" # 11 Product_AddedDate
  28. BTC = "-1" # 12 Product_BTC_SellingPrice
  29. USD = "-1" # 13 Product_USD_SellingPrice
  30. EURO = "-1" # 14 Product_EURO_SellingPrice
  31. sold = "-1" # 15 Product_QuantitySold
  32. left = "-1" # 16 Product_QuantityLeft
  33. shipFrom = "-1" # 17 Product_ShippedFrom
  34. shipTo = "-1" # 18 Product_ShippedTo
  35. image = "-1" # 19 Product_Image
  36. vendor_image = "-1" # 20 Vendor_Image
  37. # Finding Product Name
  38. name = soup.find('h1').text
  39. name = name.replace('\n', ' ')
  40. name = name.replace(",", "")
  41. name = name.strip()
  42. # Finding description
  43. desc = ''
  44. tab = soup.find('div', {"id": "tab-description"})
  45. if tab is not None:
  46. for p in tab.findAll('p'):
  47. desc += p.text
  48. if desc == '':
  49. short = soup.find('div', {"class": "woocommerce-product-details__short-description"})
  50. if short is not None:
  51. desc = short.text
  52. describe = cleanString(desc.strip())
  53. # Finding Product Image
  54. image = soup.find('div', {'class': 'woocommerce-product-gallery__wrapper'}).find('img')
  55. image = image.get('src')
  56. image = image.split('base64,')[-1]
  57. # Finding Vendor
  58. vendor = soup.find('a', {'class': 'wcfm_dashboard_item_title'}).text
  59. vendor = vendor.replace(",", "")
  60. vendor = vendor.replace("Sold by:", "")
  61. vendor = vendor.strip()
  62. # Finding Vendor Image
  63. vendor_image = soup.find('div', {'class': 'wcfmmp_sold_by_container_left'}).find('img')
  64. vendor_image = vendor_image.get('src')
  65. vendor_image = vendor_image.split('base64,')[-1]
  66. # Finding Category
  67. catSpan = soup.find('span', {'class': 'posted_in'})
  68. category = catSpan.find('a').text
  69. # Finding USD
  70. priceText = soup.find('p', {'class': 'price'}).text
  71. USD = str(priceText).strip()
  72. # Searching for CVE and MS categories
  73. cve = soup.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  74. if cve:
  75. CVE = " "
  76. for idx in cve:
  77. CVE += (idx)
  78. CVE += " "
  79. CVE = CVE.replace(',', ' ')
  80. CVE = CVE.replace('\n', '')
  81. ms = soup.findAll(text=re.compile('MS\d{2}-\d{3}'))
  82. if ms:
  83. MS = " "
  84. for im in ms:
  85. MS += (im)
  86. MS += " "
  87. MS = MS.replace(',', ' ')
  88. MS = MS.replace('\n', '')
  89. # Populating the final variable (this should be a list with all fields scraped)
  90. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  91. BTC, USD, EURO, sold, left, shipFrom, shipTo, image, vendor_image)
  92. # Sending the results
  93. return row
  94. # This is the method to parse the Listing Pages
  95. def Robinhood_listing_parser(soup):
  96. # Fields to be parsed
  97. nm = 0 # *Total_Products (Should be Integer)
  98. mktName = "RobinhoodMarket" # 0 *Marketplace_Name
  99. vendor = [] # 1 *Vendor y
  100. rating_vendor = [] # 2 Vendor_Rating
  101. success = [] # 3 Vendor_Successful_Transactions
  102. name = [] # 4 *Product_Name y
  103. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  104. MS = [] # 6 Product_MS_Classification (Microsoft Security)
  105. category = [] # 7 Product_Category y
  106. describe = [] # 8 Product_Description
  107. views = [] # 9 Product_Number_Of_Views
  108. reviews = [] # 10 Product_Number_Of_Reviews
  109. rating_item = [] # 11 Product_Rating
  110. addDate = [] # 12 Product_AddDate
  111. BTC = [] # 13 Product_BTC_SellingPrice
  112. USD = [] # 14 Product_USD_SellingPrice y
  113. EURO = [] # 15 Product_EURO_SellingPrice
  114. sold = [] # 16 Product_QuantitySold
  115. qLeft =[] # 17 Product_QuantityLeft
  116. shipFrom = [] # 18 Product_ShippedFrom
  117. shipTo = [] # 19 Product_ShippedTo
  118. image = [] # 20 Product_Image
  119. image_vendor = [] # 21 Vendor_Image
  120. href = [] # 22 Product_Links
  121. listing = soup.find('ul', {"class": "products columns-4"})
  122. items = listing.findAll('li')
  123. # Populating the Number of Products
  124. nm = len(items)
  125. for card in items:
  126. # Finding Category
  127. cat = soup.find("h1").text
  128. cat = cat.replace('\n', ' ')
  129. cat = cat.replace(",", "")
  130. cat = cat.strip()
  131. category.append(cat)
  132. bae = card.findAll('a')
  133. # Adding the url to the list of urls
  134. link = card.find('a').get('href')
  135. href.append(link)
  136. # Finding Product Name
  137. product = card.find("h2").text
  138. product = product.replace('\n', ' ')
  139. product = product.replace(",", "")
  140. product = product.strip()
  141. name.append(product)
  142. # Finding Product Image
  143. product_image = card.find('a').find('img')
  144. product_image = product_image.get('src')
  145. product_image = product_image.split('base64,')[-1]
  146. image.append(product_image)
  147. info = card.find('div', {'class': 'wcfmmp_sold_by_container'})
  148. # Finding Vendor
  149. vendor_name = info.find('a', {'class', 'wcfm_dashboard_item_title'}).text
  150. vendor_name = vendor_name.replace(",", "")
  151. vendor_name = vendor_name.strip()
  152. vendor.append(vendor_name)
  153. # Finding Vendor Image
  154. vendor_icon = info.find('img', {'class', 'wcfmmp_sold_by_logo'})
  155. vendor_icon = vendor_icon.get('src')
  156. vendor_icon = vendor_icon.split('base64,')[-1]
  157. image_vendor.append(vendor_icon)
  158. # Finding USD
  159. span = card.find('span', {'class': 'price'})
  160. if span is not None:
  161. bdi = span.find('bdi')
  162. usdText = bdi.find('span').next_sibling
  163. usdVal = usdText.text
  164. else:
  165. usdVal = "0"
  166. USD.append(usdVal)
  167. # Searching for CVE and MS categories
  168. cve = card.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  169. if not cve:
  170. cveValue="-1"
  171. else:
  172. cee = " "
  173. for idx in cve:
  174. cee += (idx)
  175. cee += " "
  176. cee = cee.replace(',', ' ')
  177. cee = cee.replace('\n', '')
  178. cveValue=cee
  179. CVE.append(cveValue)
  180. ms = card.findAll(text=re.compile('MS\d{2}-\d{3}'))
  181. if not ms:
  182. MSValue="-1"
  183. else:
  184. me = " "
  185. for im in ms:
  186. me += (im)
  187. me += " "
  188. me = me.replace(',', ' ')
  189. me = me.replace('\n', '')
  190. MSValue=me
  191. MS.append(MSValue)
  192. #print(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  193. # reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href)
  194. # Populate the final variable (this should be a list with all fields scraped)
  195. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  196. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href, image, image_vendor)
  197. def Robinhood_links_parser(soup):
  198. # Returning all links that should be visited by the Crawler
  199. href = []
  200. #list = soup.findAll('div', {"class": "woocommerce columns-4"})
  201. listing = soup.find('ul', {"class": "products columns-4"}).findAll('li')
  202. for item in listing:
  203. link = item.find('a')['href']
  204. href.append(link)
  205. return href
  206. if __name__ == '__main__':
  207. nError = 0
  208. marketPlace = 'RobinhoodMarket'
  209. lines = [] # listing pages
  210. lns = [] # description pages
  211. detPage = {}
  212. '''
  213. # reading description pages
  214. count = 0
  215. for fileDescription in glob.glob(os.path.join("..\\" + marketPlace + "\\HTML_Pages\\08082023\\Description", '*.html')):
  216. count += 1
  217. lns.append(fileDescription)
  218. # if count > 5:
  219. # break
  220. for index, line2 in enumerate(lns):
  221. print("Reading description folder of '" + marketPlace + "', file '" + os.path.basename(line2) + "', index= " + str(index + 1) + " ... " + str(len(lns)))
  222. try:
  223. html = codecs.open(line2.strip('\n'), encoding='utf8')
  224. soup = BeautifulSoup(html, "html.parser")
  225. html.close()
  226. except:
  227. try:
  228. html = open(line2.strip('\n'))
  229. soup = BeautifulSoup(html, "html.parser")
  230. html.close()
  231. except:
  232. nError += 1
  233. print("There was a problem to read the file " + line2 + " in the Description section!")
  234. # if createLog:
  235. # logFile.write(str(nError) + ". There was a problem to read the file " + line2 + " in the Description section.\n")
  236. continue
  237. try:
  238. print(Robinhood_description_parser(soup))
  239. except:
  240. traceback.print_exc()
  241. print("There was a problem to parse the file " + line2 + " in the Description section!")
  242. '''
  243. # reading listing pages
  244. count = 0
  245. for fileListing in glob.glob(os.path.join("..\\" + marketPlace + "\\HTML_Pages\\08082023\\Listing", '*.html')):
  246. count += 1
  247. lines.append(fileListing)
  248. #if count > 1:
  249. # break
  250. for index, line1 in enumerate(lines):
  251. print("Reading listing folder of '" + marketPlace + "', file '" + os.path.basename(line1) + "', index= " + str(index + 1) + " ... " + str(len(lines)))
  252. readError = False
  253. try:
  254. html = codecs.open(line1.strip('\n'), encoding='utf8')
  255. soup = BeautifulSoup(html, "html.parser")
  256. html.close()
  257. except:
  258. try:
  259. html = open(line1.strip('\n'))
  260. soup = BeautifulSoup(html, "html.parser")
  261. html.close()
  262. except:
  263. print("There was a problem to read the file " + line1 + " in the Listing section!")
  264. readError = True
  265. if not readError:
  266. parseError = False
  267. try:
  268. test = Robinhood_listing_parser(soup)
  269. print(Robinhood_listing_parser(soup))
  270. except:
  271. traceback.print_exc()
  272. print("There was a problem to parse the file " + line1 + " in the listing section!")
  273. parseError = True
  274. print("DONE")