2014-02-09 84 views
0

我是新來的使用美麗和刮一般,所以我試圖讓我的腳溼說話。python beautifulsoup4解析谷歌財務數據

我想從這裏得到的道瓊斯工業平均指數信息的第一行: http://www.google.com/finance/historical?q=INDEXDJX%3A.DJI&ei=ZN_2UqD9NOTt6wHYrAE

雖然我可以讀取數據,並打印(湯)輸出的一切,我似乎無法到放下足夠多。我將如何選擇我保存到表格中的行?第一排怎麼樣?

非常感謝您的幫助!

import urllib.parse 
import urllib.request 
from bs4 import BeautifulSoup 
import json 
import sys 
import os 
import time 
import csv 
import errno 

DJIA_URL = "http://www.google.com/finance/historical?q=INDEXDJX%3A.DJI&ei=ZN_2UqD9NOTt6wHYrAE" 

def downloadData(queryString): 
    with urllib.request.urlopen(queryString) as url: 
     encoding = url.headers.get_content_charset() 
     result = url.read().decode(encoding) 
    return result 

raw_html = downloadData(DJIA_URL) 
soup = BeautifulSoup(raw_html) 

#print(soup) 

table = soup.findAll("table", {"class":"gf-table historical_price"}) 

回答

2

你要那麼第二tr錶行:

prices = soup.find('table', class_='historical_price') 
rows = prices.find_all('tr') 
print rows[1] 

,或者列出與價格信息的所有行,跳過一個與任何th元素:

for row in rows: 
    if row.th: continue 

或使用那第一個標題作爲字典鍵的來源:

keys = [th.text.strip() for th in rows[0].find_all('th')] 
for row in rows[1:]: 
    data = {key: td.text.strip() for key, td in zip(keys, row.find_all('td'))} 
    print data 

主要生產:

{u'Volume': u'105,782,495', u'High': u'15,798.51', u'Low': u'15,625.53', u'Date': u'Feb 7, 2014', u'Close': u'15,794.08', u'Open': u'15,630.64'} 
{u'Volume': u'106,979,691', u'High': u'15,632.09', u'Low': u'15,443.00', u'Date': u'Feb 6, 2014', u'Close': u'15,628.53', u'Open': u'15,443.83'} 
{u'Volume': u'105,125,894', u'High': u'15,478.21', u'Low': u'15,340.69', u'Date': u'Feb 5, 2014', u'Close': u'15,440.23', u'Open': u'15,443.00'} 
{u'Volume': u'124,106,548', u'High': u'15,481.85', u'Low': u'15,356.62', u'Date': u'Feb 4, 2014', u'Close': u'15,445.24', u'Open': u'15,372.93'} 

+0

嗨,我應該使用.find和.find_all時? – user1357015

+0

'.find()'僅查找* first *匹配或返回'None','.find_all()'返回0個或更多匹配的列表。 –

+0

太好了。這真的有幫助。是row.th特定於beautifulsoup?以前從未見過。 – user1357015