【发布时间】:2019-02-10 19:58:10
【问题描述】:
我正在尝试从雅虎金融股票中抓取历史数据。我从互联网上找到了一些代码并对其进行了修改。起初它有效,但现在我无法从雅虎刮掉面包屑。谁能建议如何解决这个问题?
第一次刷yahoo,它刷了20组数据,就停止工作了。我又开始了,还是不行
下面附上相关代码
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)
完整的原始代码可以在以下位置找到: https://maikros.github.io/yahoo-finance-python/
提前感谢您的帮助
【问题讨论】:
-
欢迎来到 Stack Overflow。听起来雅虎财经可能阻止了你,或者你做了一个改变,破坏了抓取。稍后再试,看看它是否有效(阻塞通常是暂时的)。
标签: python yahoo-finance