Skip to content

Instantly share code, notes, and snippets.

@fredchu
Last active July 12, 2021 08:40
Show Gist options
  • Save fredchu/67490bf3513674145f0758045c49fef4 to your computer and use it in GitHub Desktop.
Save fredchu/67490bf3513674145f0758045c49fef4 to your computer and use it in GitHub Desktop.
Use pandas_datareader with user agent and session
# reference:
# _get_response without headers doesn't work (at least with 'yahoo' source #867 https://github.com/pydata/pandas-datareader/issues/867
from pandas_datareader import DataReader
import requests
USER_AGENT = {
'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
' Chrome/91.0.4472.124 Safari/537.36')
}
sesh = requests.Session()
sesh.headers.update(USER_AGENT)
tw2330 = DataReader('2330.TW', data_source='yahoo', start='2000-01-01', session=sesh)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment