I am trying to scrape the historical data from Yahoo finance stocks. I found some code from the internet and modified it. It worked at first, but now I cannot scrape the crumb from yahoo. Can anyone advise how to fix this?
The first time I scraped yahoo, it scraped 20 sets of data and stopped working. I started it again and it did't work
The relevant code is attached below
def _get_crumbs_and_cookies(stock):
"""
get crumb and cookies for historical data csv download from yahoo finance
parameters: stock - short-handle identifier of the company
returns a tuple of header, crumb and cookie
"""
url = 'https://finance.yahoo.com/quote/{}/history'.format(stock)
with requests.session():
header = {'Connection': 'keep-alive',
'Expires': '-1',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) \
AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/54.0.2840.99 Safari/537.36'
}
website = requests.get(url, headers=header)
soup = BeautifulSoup(website.text, 'lxml')
crumb = re.findall('"CrumbStore":{"crumb":"(.+?)"}', str(soup))
return (header, crumb[0], website.cookies)
And the entire original code can be found at: https://maikros.github.io/yahoo-finance-python/
Thanks in advance for your help