用Python为道琼斯指数刮雅虎财经

2024-10-16 22:31:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用Python中的靓汤从雅虎财经(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

以下是谷歌chrome的元素检查: enter image description here

我怎么能从span标签上刮出17555.47的数字?在


Tags: fromimporthttpdateyahoospanfinancesoup
1条回答
网友
1楼 · 发布于 2024-10-16 22:31:08

只需使用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'

相关问题 更多 >