beautifulsoup 标签元素内容() strip() 方法抛出 TypeError: Nonetype 不可调用 [英] beautifulsoup tag element contents() strip() method throw TypeError: Nonetype is not callable
问题描述
案例一:
<li class="chapters">
<i>In</i>
<i>vitro</i>
blahblah1
<i>in</i>
<i>vitro</i>
blahblah2
<a href="***">View details</a>
</li>
案例二:
<li class="chapters">
blahblah2
<a href="***">View details</a>
</li>
我遇到了两个问题:问题 1:当我使用 .contents[0].strip() 来获取 blahblahs 时,情况 2 有效.但会抛出 TypeError: Nonetype 不可调用.在第 1 种情况下.第一种情况的 .contents[0] 是一个标签:In.这是一个无类型吗?它是一个标签,但没有 Nonetype.问题2:我如何使用一两行来处理这两种情况.case 1st 存在的原因我猜是网站的输入错误.
I got 2 problems: Problem 1st: when I use .contents[0].strip(), to get the blahblahs, case 2nd works. but will throw TypeError: Nonetype is not callable. At case 1st. .contents[0] of case 1st is a tag: In. is this a Nonetype? it's a tag but no Nonetype. Problem 2nd: how can I use one or two lines to deal with these 2 cases. the reason why case 1st exists I guess is the input error of the website.
顺便说一下,我用的是 Beautifulsoup &lxml 解析 html.
by the way, I use the Beautifulsoup & lxml to parse the html.
推荐答案
选择 a
标签,然后使用 .previous_sibling
select a
tag then get previous content using .previous_sibling
texts = soup.select('.chapters a')
for t in texts:
print(t.previous_sibling.strip())
这篇关于beautifulsoup 标签元素内容() strip() 方法抛出 TypeError: Nonetype 不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!