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.

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