python bs4 find_all_python – 在bs4.element.ResultSet对象或列表上的beautifulsoup:find_all?...

本文探讨了如何在使用BeautifulSoup解析HTML时,对find_all方法返回的ResultSet对象进行二次查询。作者希望避免通过循环来实现这一目标,并询问是否可以直接将ResultSet转换为BeautifulSoup对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

嗨所以我在一个beautifulsoup对象上应用find_all,并找到一些东西,它是一个bs4.element.ResultSet对象或一个列表.

我想在那里进一步做find_all,但是bs4.element.ResultSet对象不允许这样做.我可以循环遍历bs4.element.ResultSet对象的每个元素来执行find_all.但我可以避免循环并将其转换回beautifulsoup对象吗?

请参阅代码了解详情.谢谢

html_1 = """

ABCD

"""

soup = BeautifulSoup(html_1,'html.parser')

type(soup) #bs4.BeautifulSoup

# do find_all on beautifulsoup object

th_all = soup.find_all('th')

# the result is of type bs4.element.ResultSet or similarly list

type(th_all) #bs4.element.ResultSet

type(th_all[0:1]) #list

# now I want to further do find_all

th_all.find_all(text='A') #not work

# can I avoid this need of loop?

for th in th_all:

th.find_all(text='A') #works

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值