【问题标题】:Getting more than 100 days of data web scraping Yahoo获取超过 100 天的数据网络抓取 Yahoo
【发布时间】:2018-12-04 03:06:45
【问题描述】:

与许多其他人一样,由于雅虎和谷歌 API 已经失效,我一直在寻找股票价格的替代来源。我决定尝试从雅虎网站上抓取历史价格仍然可用的网站。我设法将以下代码放在一起,几乎可以满足我的需要:

import urllib.request as web
import bs4 as bs

def yahooPrice(tkr):
    tkr=tkr.upper()
    url='https://finance.yahoo.com/quote/'+tkr+'/history?p='+tkr

    sauce=web.urlopen(url)
    soup=bs.BeautifulSoup(sauce,'lxml')
    table=soup.find('table')
    table_rows=table.find_all('tr')

    allrows=[]
    for tr in table_rows:
        td=tr.find_all('td')
        row=[i.text for i in td]
        if len(row)==7:
            allrows.append(row)

    vixdf= pd.DataFrame(allrows).iloc[0:-1]
    vixdf.columns=['Date','Open','High','Low','Close','Aclose','Volume']
    vixdf.set_index('Date',inplace=True)

    return vixdf

它会生成一个包含我想要的信息的数据框。不幸的是,即使实际网页显示了一整年的价格,我的例程也只返回 100 条记录(包括股息记录)。知道如何获得更多吗?

【问题讨论】:

  • 价格通过 Javascript 动态加载,beautifulsoup 不执行

标签: python screen-scraping finance stockquotes


【解决方案1】:

我相信,Yahoo Finance API 在 17 年 5 月已贬值。现在,有很多免费下载时间序列数据的选项,至少我知道。尽管如此,总有某种替代方案。查看下面的 URL 以找到下载历史价格的工具。

http://investexcel.net/multiple-stock-quote-downloader-for-excel/

也看这个。

https://blog.quandl.com/api-for-stock-data

【讨论】:

    【解决方案2】:

    我没有你的问题的确切解决方案,但我有一个解决方法(我遇到了同样的问题,因此使用了这种方法)......基本上,你可以使用 Bday() 方法 - 'import pandas.tseries .offset' 并寻找 x 个工作日来收集数据。在我的例子中,我运行了三次循环以获得 300 个工作日数据 - 我知道默认情况下我获得的最大值是 100。

    基本上,您运行循环三次并设置 Bday() 方法,以便第一次迭代获取从现在起 100 天的数据,然后是接下来的 100 天(从现在起 200 天),最后是最后 100 天(300几天后)。使用它的全部意义在于,在任何给定时间点,只能抓取 100 天的数据。所以基本上,即使你一口气循环 300 天,你也可能得不到 300 天的数据——你原来的问题(可能雅虎限制了一次提取的数据量)。我的代码在这里:https://github.com/ee07kkr/stock_forex_analysis/tree/dataGathering

    请注意,在我的情况下,由于某种原因,csv 文件不能使用 /t 分隔符......但基本上你可以使用数据框。我目前遇到的另一个问题是“音量”是一个字符串而不是浮点数......解决方法是:

    apple = pd.DataFrame.from_csv('AAPL.csv',sep ='\t') apple['Volume'] = apple['Volume'].str.replace(',','').astype(float)

    【讨论】:

    • 感谢您的回复。这听起来对我有用,但我担心我错过了一些东西。如果你已经知道你想要 300 天,我不明白 pandas.tseries.offset 的目的。同样的 100 天不会打 3 次就得到 3 份吗?您能否发布一个简短的示例来澄清一下?谢谢
    【解决方案3】:

    首先 - 运行以下代码以获得 100 天。 然后 - 使用 SQL 将数据插入到一个小数据库中(Sqlite3 很容易与 python 一起使用)。 最后 - 修改下面的代码以获取每日价格,您可以添加这些价格来扩展您的数据库。

    from pandas import DataFrame
    import bs4
    import requests
    
    def function():
        url = 'https://uk.finance.yahoo.com/quote/VOD.L/history?p=VOD.L'
        response = requests.get(url)
        soup=bs4.BeautifulSoup(response.text, 'html.parser')
        headers=soup.find_all('th')
        rows=soup.find_all('tr')
        ts=[[td.getText() for td in rows[i].find_all('td')] for i in range (len(rows))]
        date=[]
        days=(100)
        while days > 0:
            for i in ts:
                data.append (i[:-6])
            now=data[num]
            now=DataFrame(now)
            now=now[0]
    
            now=str(now[0])
            print now, item
            num=num-1
    

    【讨论】:

      猜你喜欢
      • 2021-05-23
      • 1970-01-01
      • 2016-12-13
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 2014-07-10
      • 1970-01-01
      相关资源
      最近更新 更多