2016-08-12 136 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返回所有匹配 –

相关问题