Python&美丽的汤 - 搜索结果字符串
问题描述:
我正在使用美味汤来解析HTML表格。Python&美丽的汤 - 搜索结果字符串
- Python版本3.2
- 美丽的汤版本4.1.3
试图使用的findAll方法,以我的行内找到列时,我遇到了一个问题。我得到一个错误,说列表对象没有属性findAll。我通过堆栈交换中的另一篇帖子发现了这种方法,这在那里并不是问题。 (BeautifulSoup HTML table parsing)
我意识到findAll是BeautifulSoup的一种方法,不是Python列表。奇怪的部分是findAll方法在找到表列表中的行(我只需要页面上的第二个表)时工作,但是当我尝试在行列表中查找列时。
这里是我的代码:
from urllib.request import URLopener
from bs4 import BeautifulSoup
opener = URLopener() #Open the URL Connection
page = opener.open("http://www.labormarketinfo.edd.ca.gov/majorer/countymajorer.asp?CountyCode=000001") #Open the page
soup = BeautifulSoup(page)
table = soup.findAll('table')[1] #Get the 2nd table (index 1)
rows = table.findAll('tr') #findAll works here
cols = rows.findAll('td') #findAll fails here
print(cols)
答
findAll()
返回结果列表,你需要循环的或选择一个去与它自己的findAll()
方法的另一个包含的元素:
table = soup.findAll('table')[1]
rows = table.findAll('tr')
for row in rows:
cols = rows.findAll('td')
print(cols)
或挑一个行:
table = soup.findAll('table')[1]
rows = table.findAll('tr')
cols = rows[0].findAll('td') # columns of the *first* row.
print(cols)
请注意,findAll
已被弃用,您应该使用find_all()
来代替。
谢谢你的作品。 – fb20009 2013-04-11 14:52:06