2013-06-04 43 views
0

我試圖從http://www.footywire.com/afl/footy/ft_match_statistics?mid=5634 從BeautifulSoupBeautifulSoup沒有返回源

下載表格的數據,但遇到問題時,我嘗試並獲得湯

我想

URL =「http://www.footywire.com/afl/footy/ft_match_statistics?mid=5634

湯= BeautifulSoup(url)

但只是回頭,或根本沒有。我也嘗試過使用不同的解析器(html5lib),並且還通過urllib2讀取頁面,但仍未獲取任何頁面正文。我在網絡互動中很沒用,所以也許有一些基本的東西我缺少,但它似乎在其他網站上工作。

任何幫助將不勝感激拉動這些數據。爲什麼我沒有獲得預期的來源?

回答

0

你好澳洲朋友:)

如果我是你,我會使用請求和lxml。我認爲該網站正在檢查cookie和一些標題。請求會話類存儲cookie,並且可以讓你傳遞標題。 lxml會讓你在這裏使用xpath,我認爲它比BeautifulSoup的界面更不痛苦。

見下文:

>>> import lxml.html 
>>> import requests 
>>> session = requests.session() 
>>> response = session.get("http://www.footywire.com/afl/footy/ft_match_statistics?mid=5634", headers={"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36","Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Referer":"http://www.footywire.com/afl/footy/ft_match_statistics?mid=5634","Cache-Control":"max-age=0"}) 
>>> tree = lxml.html.fromstring(response.text) 
>>> rows = tree.xpath("//table//table//table//table//table//table//tr") 
>>> for row in rows: 
...  row.xpath(".//td//text()") 
... 
[u'\xa0\xa0', 'Sydney Match Statistics (Sorted by Disposals)', 'Coach: ', 'John Longmire', u'\xa0\xa0'] 
['Player', 'K', 'HB', 'D', 'M', 'G', 'B', 'T', 'HO', 'I50', 'FF', 'FA', 'DT', 'SC'] 
['Josh Kennedy', '20', '17', '37', '2', '1', '1', '1', '0', '3', '1', '0', '112', '126'] 
['Jarrad McVeigh', '23', '11', '34', '1', '0', '0', '2', '0', '5', '1', '1', '100', '116'] 
... cont... 

XPath查詢可能有點脆,但你的想法:)