使用 Python 3 从 Yahoo Finance 获取价格以外的网络抓取信息 [英] Web scraping information other than price from Yahoo Finance in Python 3

查看:73
本文介绍了使用 Python 3 从 Yahoo Finance 获取价格以外的网络抓取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,因此对于任何新手错误,我深表歉意.我按照教程从 python 中抓取股票价格,但在修复它以在 python 3 中工作后,当我尝试将其适应雅虎财经页面的其他元素(例如市盈率和 Beta)时,输出只是空方括号.

I'm new to python so I apologize for any rookie mistakes. I followed a tutorial to scrape stock prices from python but after fixing it to work in python 3 when I tried to adapt it to other elements of the Yahoo Finance page such as P/E ratios and Beta the output was just empty square brackets.

import urllib.request
import re

symbolslist = ["aapl","spy","goog","nflx"]

i=0
while i<len(symbolslist):
    url = "http://finance.yahoo.com/q?s=" +symbolslist[i] +"&q1=1"
    htmlfile = urllib.request.urlopen(url)
    htmltext = htmlfile.read()
    regex = b'<th scope="row" width="48%">"P/E "<span class="small">(ttm)</span>:    </th><td class="yfnc_tabledata1">(.+?)</td>'
    pattern = re.compile(regex)
    price_to_earnings = str(re.findall(pattern,htmltext))
    print ("The price to earnings of " + symbolslist[i]+ " is " + price_to_earnings)
    i+=1

这是输出

    The price to earnings of aapl is []
    The price to earnings of spy is []
    The price to earnings of goog is []
    The price to earnings of nflx is []
    >>> 

推荐答案

我遇到了同样的问题,当我去 http://download.finance.yahoo.com,我被重定向到 http://finance.yahoo.com,看来 CSV 格式的链接已被雅虎关闭.

I am having the same problem and when I go to http://download.finance.yahoo.com, I get redirected to http://finance.yahoo.com, and it appears the CSV format link got shut down by Yahoo.

问题似乎是网址太长且复杂.也许他们这样做了,所以我们不能继续以这种方式废弃他们的数据.有没有不同的方法来解决这个问题?我也尝试从 Finance.msn.com 中删除,但遇到了同样的问题,即 url 过于复杂和冗长.

The issues seems to be that the url is too long and convoluted. Which maybe they did so we couldn't keep scrapping their data in this way. Is there a different way to go about this? I try scrapping from finance.msn.com as well, but ran into the same issue where the url is too convoluted and long.

也许我只需要寻找另一个鲜为人知的金融网站.我会看看我能找到什么.

Perhaps I just need to look for another finance site that is less known. I'll see what I can find.

这篇关于使用 Python 3 从 Yahoo Finance 获取价格以外的网络抓取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆