jean jean - 1 year ago 245
Python Question

BeautifulSoup: 'ResultSet' object has no attribute 'find_all'

from bs4 import BeautifulSoup
import urllib2

url = ''
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'

I want to grasp the table into a CSV file.
How to go ahead?

This is the table:

[<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]

Answer Source

Try this:

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

Since find_all appears to return a Python list, you were trying to call find_all on a list, which doesn't have such a method.

To get your resulting list(rows) into CSV format.. well, that depends. If you just want it on screen, you could do:


If you wanted it in a file, you would need to open a file and write the above line to it. But there are also Python modules that deal with creating CSV content. It's a different question really.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download