Python Beautifulsoup标记-查找所有成功但查找失败

Python Beautifulsoup标记-查找所有成功但查找失败,python,beautifulsoup,Python,Beautifulsoup,我遇到一个问题,特定标记h2的soup.find_all成功,但soup.find指定文本失败 我需要找到h2标签与各种文字,如介绍,结果等,如所附图片所示 谁能给点建议吗?多谢各位 print(soup.find_all('h2')) [<h2 class="Heading">Abstract</h2>, <h2 class="Heading" data-role="collapsible-handle" tabindex="-1">Introductio

我遇到一个问题,特定标记h2的soup.find_all成功,但soup.find指定文本失败

我需要找到h2标签与各种文字,如介绍,结果等,如所附图片所示

谁能给点建议吗?多谢各位

print(soup.find_all('h2'))
[<h2 class="Heading">Abstract</h2>, 
<h2 class="Heading" data-role="collapsible-handle" tabindex="-1">Introduction<span class="section-icon"></span></h2>, 
<h2 class="Heading" data-role="collapsible-handle" tabindex="-1">Patients and methods<span class="section-icon"></span></h2>, 
<h2 class="Heading" data-role="collapsible-handle" tabindex="-1">Results<span class="section-icon"></span></h2>, 
<h2 class="Heading" data-role="collapsible-handle" tabindex="-1">Discussion<span class="section-icon"></span></h2>, 
<h2 class="Heading" data-role="collapsible-handle" id="copyrightInformation" tabindex="-1">Copyright information<span class="section-icon"></span></h2>, 
<h2 class="Heading" data-role="collapsible-handle" id="aboutarticle" tabindex="-1">About this article<span class="section-icon"></span></h2>, 
<h2 class="u-isVisuallyHidden">Article actions</h2>, <h2 class="u-h4 u-jsIsVisuallyHidden">Article contents</h2>, 
<h2 class="u-isVisuallyHidden">Cookies</h2>]

print(soup.find('h2', text='Introduction'))
None
print(soup.find_all('h2'))
[摘要,
介绍
患者和方法,
结果,
讨论,
版权信息,
关于这篇文章,,
条款行动、条款内容、,
饼干]
打印(soup.find('h2',text='Introduction'))
没有一个
试试这个:

soup.find(lambda el: el.name == "h2" and "Introduction" in el.text)
试试这个:

soup.find(lambda el: el.name == "h2" and "Introduction" in el.text)

text='Introduction'
搜索
可导航字符串
,而不是
标记

从文档中:

text是一个参数,用于搜索NavigableString对象 而不是标签

你应该试试:

print(soup.find(text='Introduction').parent)

text='Introduction'
搜索
可导航字符串
,而不是
标记

从文档中:

text是一个参数,用于搜索NavigableString对象 而不是标签

你应该试试:

print(soup.find(text='Introduction').parent)

当我们使用
text/string
作为过滤器时,引擎盖下发生的事情是我们使用
tag.string
获取文本并与过滤器进行比较,在这种情况下:

import bs4

html = '''<h2 class="Heading" data-role="collapsible-handle" tabindex="-1">Introduction<span class="section-icon"></span></h2>'''
soup = bs4.BeautifulSoup(html,'lxml')
print(soup.h2.string)
为什么字符串返回
None

如果标签包含多个内容,那么不清楚是什么 .string应引用,因此.string定义为无:

h2
标记包含带有空文本的
span
标记,它很混乱,将返回
None

@Thomas Lehoux的答案是正确的方法

这是BS3 API:

findNextSiblings(name, attrs, text, limit, **kwargs)
这是BS4 API:

find_next_siblings(name, attrs, string, limit, **kwargs)
您会注意到,旧的使用
文本
,当前的使用
字符串
,但它们都一样,都使用
标记。字符串
来获取值,您可以同时使用它们。BS4只是采用了旧的格式,仅此而已

我在两个版本中都找不到任何
tag.text
API,但它的行为类似于
tag.get_text()
,它将标记下的所有文本连接起来

就你而言:

soup.h2.string    >>>  None
soup.h2.text      >>>  Introduction
soup.h2.get_text()>>>  Introduction
简言之:

text in filter is tag.string
text in tag itself is tag.text

我认为您在实践中使用了
find(string='')
,这样比较容易混淆

当我们使用
text/string
作为过滤器时,引擎盖下发生的事情是我们使用
tag.string
获取文本并与过滤器进行比较,在这种情况下:

import bs4

html = '''<h2 class="Heading" data-role="collapsible-handle" tabindex="-1">Introduction<span class="section-icon"></span></h2>'''
soup = bs4.BeautifulSoup(html,'lxml')
print(soup.h2.string)
为什么字符串返回
None

如果标签包含多个内容,那么不清楚是什么 .string应引用,因此.string定义为无:

h2
标记包含带有空文本的
span
标记,它很混乱,将返回
None

@Thomas Lehoux的答案是正确的方法

这是BS3 API:

findNextSiblings(name, attrs, text, limit, **kwargs)
这是BS4 API:

find_next_siblings(name, attrs, string, limit, **kwargs)
您会注意到,旧的使用
文本
,当前的使用
字符串
,但它们都一样,都使用
标记。字符串
来获取值,您可以同时使用它们。BS4只是采用了旧的格式,仅此而已

我在两个版本中都找不到任何
tag.text
API,但它的行为类似于
tag.get_text()
,它将标记下的所有文本连接起来

就你而言:

soup.h2.string    >>>  None
soup.h2.text      >>>  Introduction
soup.h2.get_text()>>>  Introduction
简言之:

text in filter is tag.string
text in tag itself is tag.text

我认为您在实践中使用了
find(string='')
,这样比较容易混淆

请显示您用作输入的HTML文档。谢谢更多帮助:。请显示用作输入的HTML文档。谢谢更多帮助:“
span
中的“简介”文本可以位于任何标记中。OP没有尝试使用
text='Introduction'
搜索标记,您一定误解了问题。关于Navigablesting,您是对的,但在这种情况下,它是
无的
,因为
h2
标记包含另一个标记“简介”文本可以在任何标签中(例如在
span
中)。OP没有试图搜索带有
text='Introduction'
的标记,你一定误解了这个问题。关于Navigablesting,你是对的,但在这种情况下,它是
None
,因为
h2
标记包含另一个标记谢谢。我没有意识到它会被span标记混淆。是的,我认为@Thomas Lehoux的approach很接近。但是我想要h2标记,其中字符串纯粹是“简介”。除了添加正则表达式来剥离span标记外,还有其他方法吗?@Michael Lam
soup.find(lambda el:el.name==“h2”和el.text==“简介”).text
,因为span标记的文本为空,text将返回h2标记的确切字符串。为什么使用text==“Introduction”传入lambda函数与使用.find(“h2”,text=“Introduction”)的工作方式不同?谢谢。我没有意识到它会被span标记弄糊涂。是的,我认为@Thomas Lehoux的方法很接近。但是我想要h2标记,其中字符串纯粹是“简介”。除了添加正则表达式来剥离span标记外,还有其他方法吗?@Michael Lam
soup.find(lambda el:el.name==“h2”和el.text==“简介”).text
,因为span标记的文本为空,text将返回h2标记的确切字符串。为什么使用text==“Introduction”传入lambda函数与使用.find(“h2”,text=“Introduction”)的工作方式不同?谢谢。但是,我还需要h2标记为纯“Introduction”,而不匹配其他字符串,例如“文档简介“例如,除了添加正则表达式来去除span标记外,还有其他方法吗?
soup.find(lambda el:el.name==“h2”和el.text==“简介”)
谢谢。这是一个很好的解决方案。使用text=“简介”时,该方法是如何解决问题的“就像问题中说的,你没有吗?在我看来,两人都在寻找文本变量为“简介”。谢谢。但是,我还需要h2标记完全是“简介”,而不需要它匹配其他字符串,例如“文档简介”。除了向strip th添加正则表达式之外