gpt4 book ai didi

Python Beautiful Soup 'NavigableString' 对象没有属性 'get_text'

转载 作者:行者123 更新时间:2023-12-03 13:33:20 25 4
gpt4 key购买 nike

我正在尝试从以下 html 结构中提取文本:

<div class="account-places">
<div>
<ul class="location-history">
<li></li>
<li>Text to extract</li>
</ul>
</div>
</div>

我有以下 BeautifulSoup 代码来做到这一点:
from bs4 import BeautifulSoup as bs

soup = bs(html, "lxml")
div = soup.find("div", {"class": "account-places"})
text = div.div.ul.li.next_sibling.get_text()

但是 Beautiful Soup 抛出错误:'NavigableString' 对象没有属性 'get_text'。我究竟做错了什么?

最佳答案

看来您需要 find_next_sibling("li") .

例如:

from bs4 import BeautifulSoup as bs

soup = bs(html, "lxml")
div = soup.find("div", {"class": "account-places"})
text = div.div.ul.li.find_next_sibling("li").get_text()
print(text)

输出:
Text to extract

关于Python Beautiful Soup 'NavigableString' 对象没有属性 'get_text',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50705372/

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