gpt4 book ai didi

html - BeautifulSoup : extracting from deeply nested
's

转载 作者:行者123 更新时间:2023-12-04 10:28:13 25 4
gpt4 key购买 nike

尝试从中提取消息文本:

<div class="Item ItemDiscussion Role_Member" id="Discussion_2318">
<div class="Discussion">
<div class="Item-BodyWrap">
<div class="Item-Body">
<div class="Message">
Hello<br/>I have a very interesting observation on nature of birds in Alaska ... <br/>
Was there 10/19/18 has anyone heard of this </div>
<div class="ReactionRecord"></div><div class="Reactions"></div> </div>
</div>
</div>
</div>

我得到了这个:

tag = soup.find('div', {'class' : 'ItemDiscussion'})

接下来我要尝试:

s = str((tag.contents)[1])
sp = BeautifulSoup(s)
sp.contents

但这并没有多大帮助。如何从 <div class="Message"> 获取消息文本?

最佳答案

可以直接从soup中找到元素。

discussion_div = soup.find("div", {"class": "ItemDiscussion"})
message_text = discussion_div.find("div", {"class": "Message"}).text

关于html - BeautifulSoup : extracting from deeply nested <div>'s,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60541493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com