2016-08-12 2 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은 '행'에 포함 된 것보다 많습니다 ... – Totem

답변

0

보십시오이 : 당신은하지 않는하는 listfind_all를 호출하려고했던

find_all 파이썬 list을 반환 보이므로
rows = table[0].find_all('tr') 

그러한 방법.

결과 목록 (rows)을 CSV 형식으로 가져 오려면 그럴 수 있습니다. 그냥 화면에 원하는 경우에, 당신은 할 수 있습니다 : 당신이 파일을 원한다면, 당신은 파일을 열고 위의 라인을 쓸 필요가

','.join(rows) 

. 그러나 CSV 콘텐츠를 만드는 데 사용되는 파이썬 모듈도 있습니다. 정말로 다른 질문입니다.

+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은 모든 일치 항목을 반환합니다 –