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