Issue
I asked a question earlier regarding an error I was getting. It helped by adding the correct class name for the table on this webpage. However, the code runs but when i print(data), I get an empty response.
url = "https://coinmarketcap.com/currencies/bitcoin/historical-data/"
content = requests.get(url).content
soup = BeautifulSoup(content,'html.parser')
table = soup.find('table', {'class': 'cmc-table'})
data = [[td.text.strip() for td in tr.findChildren('td')]
for tr in table.findChildren('tr')]
print(data)
output:
C:\Users\Ejer\anaconda3\envs\pythonProject\python.exe C:/Users/Ejer/PycharmProjects/pythonProject/stock_analysis.py
[[], ['No Data']]
Process finished with exit code 0
Solution
What happens?
Website deals with dynamic content and table is empty in the moment you try to grab the html.
Solution
from selenium import webdriver
from bs4 import BeautifulSoup
from time import sleep
browser = webdriver.Chrome('C:\Program Files\ChromeDriver\chromedriver.exe')
browser.get('https://coinmarketcap.com/currencies/bitcoin/historical-data/')
sleep(2)
soup = BeautifulSoup(browser.page_source,'html.parser')
browser.close()
table = soup.find('table', {'class': 'cmc-table'})
[[td.text.strip() for td in tr.findChildren('td')] for tr in table.findChildren('tr')][1:]
Output
[['Jan 09, 2021',
'$40,788.64',
'$41,436.35',
'$38,980.88',
'$40,254.55',
'$61,984,162,837',
'$748,563,483,043'],
['Jan 08, 2021',
'$39,381.77',
'$41,946.74',
'$36,838.64',
'$40,797.61',
'$88,107,519,480',
'$758,625,941,267'],
[...]]
Answered By - HedgeHog
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.