【发布时间】:2013-11-30 03:12:32
【问题描述】:
我正在尝试自学如何通过网络抓取股票数据。我是一个新手,所以请原谅我可能会问的任何愚蠢的问题。
这是我的价格抓取代码,我也在尝试抓取 PE 比率。
import urllib.request
from bs4 import BeautifulSoup
start = 'http://www.google.com/finance?cid=694653'
page = urllib.request.urlopen(start)
soup = BeautifulSoup(page)
P = soup.find('span',{'id':'ref_694653_l'})
print(P.get_text())
pe = soup.find_all('td',{'class':'val'})
print(pe[5].get_text())
pe = soup.find('td',{'data-snapfield':'pe_ratio'})
print(pe.td.next_sibling.get_text())
我可以得到价格数据,我设法得到了市盈率,但不是直接的。我尝试使用 next_sibling 和 next_element 但它给了我一个错误,说没有属性。
我无法弄清楚如何从表中抓取数据,因为它通常是按行设置的,并且数据周围的类通常非常常见,例如 or 。
所以只是想寻求一些帮助来抓取市盈率。
谢谢大家
是的
【问题讨论】:
标签: python beautifulsoup screen-scraping