'Python Pandas how to get f-string to work in an url
I am getting an error of urllib.error.HTTPError: HTTP Error 404: Not Found. I feel like f-string should work for the url but it is not how would I fix this?
import pandas as pd
stockslist = ['f','goog', 'aapl']
for s in stockslist:
url = f'https://finance.yahoo.com/quote/{s}/'
print(url)
tablelist = pd.read_html(url, flavor='html5lib')
df = pd.concat(tablelist[:2])
print(df)
Solution 1:[1]
you have to parse html first then read it
try it:
import pandas as pd
import requests
stockslist = ['f','goog', 'aapl']
for s in stockslist:
print(s)
url = f'https://finance.yahoo.com/quote/{s}/'
html = requests.get(url).content
tablelist = pd.read_html(html, flavor='html5lib')
df = pd.concat(tablelist[:2])
print(df)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
