2016-08-12 66 views
0
from bs4 import BeautifulSoup 
import urllib2 

url = 'http://www.data.jma.go.jp/obd/stats/etrn/view/monthly_s3_en.php?block_no=47401&view=1' 
html = urllib2.urlopen(url).read()   
soup = BeautifulSoup(html) 
table = soup.find_all('table', class_='data2_s') 
rows = table.find_all('tr') 
print rows 

rows = table.find_all('tr') 
AttributeError: 'ResultSet' object has no attribute 'find_all' 

我想把這個表格轉換成一個CSV文件。 如何繼續?BeautifulSoup:'ResultSet'對象沒有屬性'find_all'

這是表:

[<table class="data2_s"><caption class="m">WAKKANAI\xa0\xa0\xa0WMO Station ID:47401\xa0Lat\xa045<sup>o</sup>24.9'N\xa0\xa0Lon\xa0141<sup>o</sup>40.7'E</caption><tr><th scope="col">Year</th><th scope="col">Jan</th><th scope="col">Feb</th><th scope="col">Mar</th><th scope="col">Apr</th><th scope="col">May</th><th scope="col">Jun</th><th scope="col">Jul</th><th scope="col">Aug</th><th scope="col">Sep</th><th scope="col">Oct</th><th scope="col">Nov</th><th scope="col">Dec</th><th scope="col">Annual</th></tr><tr class="mtx" style="text-align:right;"><td style="text-align:center">1938</td><td class="data_0_0_0_0">-5.2</td><td class="data_0_0_0_0">-4.9</td><td class="data_0_0_0_0">-0.6</td><td class="data_0_0_0_0">4.7</td><td class="data_0_0_0_0">9.5</td><td class="data_0_0_0_0">11.6</td><td class="data_0_0_0_0">17.9</td><td class="data_0_0_0_0">22.2</td><td class="data_0_0_0_0">16.5</td><td class="data_0_0_0_0">10.7</td><td class="data_0_0_0_0">3.3</td><td class="data_0_0_0_0">-4.7</td><td class="data_0_0_0_0">6.8</td></tr>\n<tr class="mtx" style="text-align:right;"><td style="text-align:center">1939</td><td class="data_0_0_0_0">-7.5</td><td class="data_0_0_0_0">-6.6</td><td class="data_0_0_0_0">-1.4</td><td] 
+1

不清楚你的問題是什麼......「沒有工作」是不夠的信息 – Totem

+0

@Totem嘿我得到了AttributeError提到的問題。 – jean

+0

好的,我認爲這個錯誤告訴你什麼是錯誤的。ResultSet很可能包含在'rows'中...... – Totem

回答

0

試試這個:

rows = table[0].find_all('tr') 

由於find_all自然會返回一個Python list,你試圖調用find_alllist,不具有這樣的方法。

爲了得到您的結果列表(rows)爲CSV格式..好,這取決於。如果你只是希望它在屏幕上,你可以這樣做:

','.join(rows) 

如果你在一個文件中想要它,你需要打開一個文件,上面的行寫入。但也有處理創建CSV內容的Python模塊。這真的是一個不同的問題。

+0

你可能會對這裏感興趣:http://stackoverflow.com/questions/38917958/convert-html-into-csv – jean

+0

你可能會對這裏感興趣:http: //stackoverflow.com/questions/38917958/convert-html-into-csv – jean

+0

當你想要一個標籤時,你應該使用'find',find_all返回所有匹配 –

相關問題