Shane S
Shane S

Reputation: 2303

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)

Upvotes: 0

Views: 683

Answers (1)

Ashkan Goleh Pour
Ashkan Goleh Pour

Reputation: 522

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)

Upvotes: 3

Related Questions