【问题标题】:Scraping Yahoo Finance for Dow Index in Python在 Python 中为道琼斯指数抓取雅虎财经
【发布时间】:2014-11-06 22:51:58
【问题描述】:

我正在尝试使用 Python 中的 Beautiful Soup 从 Yahoo Finance 抓取道琼斯股票指数。

这是我尝试过的:

from bs4 import BeautifulSoup

myurl = "http://finance.yahoo.com/q/cp?s=^DJI"
soup = BeautifulSoup(html)

for item in soup:
    date = row.find('span', 'time_rtq_ticker').text.strip()
    print date

这是来自谷歌浏览器的元素检查:

我怎样才能只从 span 标签中抓取 17,555.47 数字?

【问题讨论】:

  • 您是想学习如何使用 BeautifulSoup 进行抓取,还是只是对数据感兴趣?如果您想要数据,我相信他们的api 可能是更好的来源。
  • 我只是想从雅虎财经上刮下这个号码。干杯
  • 如果您没有声明变量“html”,您对“soup = BeautifulSoup(html)”的调用可能会返回错误。它应该读“myurl”而不是“html”吗?

标签: python indexing web-scraping beautifulsoup


【解决方案1】:

只需使用find,确实很简单,像这样:

from bs4 import BeautifulSoup
import requests

myurl = "http://finance.yahoo.com/q/cp?s=^DJI"
# I use requests to get the html content
html = requests.get(myurl).content
soup = BeautifulSoup(html)

# you don't need to iterate the children, just use find
# and you need to use attrs { key: value }, not just 'time_rtq_ticker'
soup.find('span', attrs={'class':'time_rtq_ticker'}).text
u'17,554.47'

【讨论】:

  • 正是我想要的。非常感谢!
猜你喜欢
  • 2011-04-10
  • 1970-01-01
  • 1970-01-01
  • 2020-04-01
  • 2020-09-10
  • 2017-01-14
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多