Bri*_*ley 8 python beautifulsoup
我正在尝试从以下 html 结构中提取文本:
<div class="account-places">
<div>
<ul class="location-history">
<li></li>
<li>Text to extract</li>
</ul>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我有以下 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()
Run Code Online (Sandbox Code Playgroud)
但是 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)
Run Code Online (Sandbox Code Playgroud)
输出:
Text to extract
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7424 次 |
最近记录: |