Python 3.x Python&;美丽的汤-搜索结果字符串
我正在使用BeautifulSoup解析HTML表Python 3.x Python&;美丽的汤-搜索结果字符串,python-3.x,beautifulsoup,Python 3.x,Beautifulsoup,我正在使用BeautifulSoup解析HTML表 Python版本3.2 靓汤4.1.3版 我在尝试使用findAll方法查找行中的列时遇到了一个问题。我得到一个错误,说列表对象没有findAll属性。我在stack exchange上的另一篇文章中发现了这种方法,但这并不是一个问题。() 我意识到findAll是一种美化组的方法,而不是python列表。奇怪的是,当我在表列表中找到行时(我只需要页面上的第二个表),但当我试图在行列表中找到列时,findAll方法就起作用了 这是我的密码:
- Python版本3.2
- 靓汤4.1.3版
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()
table = soup.findAll('table')[1]
rows = table.findAll('tr')
for row in rows:
cols = rows.findAll('td')
print(cols)
或者选择一行:
请注意,findAll
已被弃用,您应该改用find_all()
。findAll()
返回一个结果列表,您需要循环这些结果列表,或者选择一个以使用自己的findAll()
方法访问另一个包含的元素:
table = soup.findAll('table')[1]
rows = table.findAll('tr')
for row in rows:
cols = rows.findAll('td')
print(cols)
或者选择一行:
请注意,findAll
已被弃用,您应该改用find_all()