Philip McQuitty
Philip McQuitty

Reputation: 1077

Scraping Yahoo Finance for Dow Index in Python

I am trying to scrape the Dow Jones stock index from Yahoo Finance with Beautiful Soup in Python.

Here is what I have tried:

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

Here is the element inspection from google chrome: enter image description here

How can I only scrape the 17,555.47 number from the span tags?

Upvotes: 3

Views: 3166

Answers (1)

Anzel
Anzel

Reputation: 20563

Just use find, very easy indeed, like this:

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'

Upvotes: 3

Related Questions