2016-08-12 11 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

[OK]を、私はエラーが何か間違っていると言っていると思う.. ResultSetはより多くの可能性が含まれている '行' ... – Totem

答えて

0

このお試しください:find_all以来

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

は、Python listを返すように見えますが、あなたは持っていませんlist、上find_allを呼び出すようにしようとしていましたそのような方法。

結果リスト(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はすべての一致を返します –

関連する問題