1
我使用的是從布爾「你好Python的!」下面的代碼:用美麗的湯在Python屬性「內容」
import urllib2
from bs4 import BeautifulSoup
import os
def get_stock_html(ticker_name):
opener = urllib2.build_opener(urllib2.HTTPRedirectHandler(),urllib2.HTTPHandler(debuglevel=0),)
opener.addhaders = [('User-agent', "Mozilla/4.0 (compatible; MSIE 7.0; " "Windows NT 5.1; .NET CLR 2.0.50727; " ".NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)")]
url = "http://finance.yahoo.com/q?s=" + ticker_name
response = opener.open(url)
return ''.join(response.readlines())
def find_quote_section(html):
soup = BeautifulSoup(html)
# quote = soup.find('div', attrs={'class': 'yfi_rt_quote_summary_rt_top'})
quote = soup.find('div', attrs={'class': 'yfi_quote_summary'})
return quote
def parse_stock_html(html, ticker_name):
quote = find_quote_section(html)
result = {}
tick = ticker_name.lower()
result['stock_name'] = quote.find('h2').contents[0]
if __name__ == '__main__':
os.system("clear")
html = get_stock_html('GOOG')
# print find_quote_section(html)
print parse_stock_html(html, 'GOOG')
得到以下錯誤:
Traceback (most recent call last):
File "dwlod.py", line 33, in <module>
print parse_stock_html(html, 'GOOG')
File "dwlod.py", line 25, in parse_stock_html
result['stock_name'] = quote.find('h2').contents[0]
AttributeError: 'NoneType' object has no attribute 'contents'
我一個新手,真的不知道該怎麼做。這本書錯了嗎?
ADDED
我剛剛更換result['stock_name'] = quote.find('h2').contents[0]
有:
x = BeautifulSoup(html).find('h2').contents[0]
return x
現在,沒有東西返回,但錯誤不再出現。。那麼,原始的python語法有什麼問題嗎?
很可能API在書寫和現在變更之間 –
@DavidRobinson謝謝!我在哪裏可以找到正確的API? – dwstein
好問題。我建議你看看雅虎頁面(它所查詢的URL)返回的內容(我會在移動設備上)。你是否熟悉BeautifulSoup來改變它? –