Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 结合;查找“;在豆腐汤中有作用_Python_Beautifulsoup - Fatal编程技术网

Python 结合;查找“;在豆腐汤中有作用

Python 结合;查找“;在豆腐汤中有作用,python,beautifulsoup,Python,Beautifulsoup,我试图从下面的html源代码中获取标签列表,这些标签是CTC_3D_打印机、CTC_prusa_i3_pro_b、CTC_升级 html = """ <div class="content_stack"> <h2 class="section-header justify"> Tags </h2> <div class="thing-detail-tags-container">

我试图从下面的html源代码中获取标签列表,这些标签是CTC_3D_打印机、CTC_prusa_i3_pro_b、CTC_升级

 html = """
<div class="content_stack">
    <h2 class="section-header justify">
        Tags

            </h2>


    <div class="thing-detail-tags-container">
        <div class="taglist">
                    <a href="/tag:CTC_3D_Printer">CTC_3D_Printer</a>
                    <a href="/tag:ctc_prusa_i3_pro_b">ctc_prusa_i3_pro_b</a>
                    <a href="/tag:CTC_Upgrades">CTC_Upgrades</a>
    </div>
    </div>
</div>



    <div class="content_stack">
        <h2 class="section-header">
            Design Tools
        </h2>

        <div class="taglist">
                    <span>Tinkercad</span>
                </div>
    </div>
"""
去拿标签。然而,由于标签周围有额外的空间,我不能使用它。标签并不总是在
后面的第一个元素。通过将“find”与一些预定义函数结合起来,我如何解决我的问题呢?

如文档中所述,您只需编写一个函数(它接受一个BS标记对象,如果匹配则返回true),并将其传递给
find

他们的示例是一个函数,该函数只查找带有
类但没有
id的标记

def has_class_but_no_id(tag):
    return tag.has_attr('class') and not tag.has_attr('id')
对于您的情况,您只需要在
中对文本进行检查:

h2 = soup.find('h2', string=lambda s: 'Tags' in s)
……或者:

h2 = soup.find(lambda tag: tag.name=='h2' and 'Tags' in tag.string)
如文档中所述,您只需编写一个函数(接受BS tag对象并在匹配时返回true),然后将其传递给
find

他们的示例是一个函数,该函数只查找带有
类但没有
id的标记

def has_class_but_no_id(tag):
    return tag.has_attr('class') and not tag.has_attr('id')
对于您的情况,您只需要在
中对文本进行检查:

h2 = soup.find('h2', string=lambda s: 'Tags' in s)
……或者:

h2 = soup.find(lambda tag: tag.name=='h2' and 'Tags' in tag.string)

附带说明:为什么要使用
findNextSibling
?您是在使用BS3,还是在试图从为BS3编写的教程、书籍或示例代码中学习BS4?BS3已经被抛弃了五年多了(甚至在BS3中,3.2也提供了BS4样式的名称以简化移植)。哦,我应该用什么来代替FindNext?实际上,我是通过看别人的脚本来学习BS的,所以…看看更新的脚本,而不是6年前的脚本。或者,最好看看文档中的例子。你的问题是什么@edyvedy13?您没有尝试获取这些标签吗?作为旁注:为什么要使用
findNextSibling
?您是在使用BS3,还是在试图从为BS3编写的教程、书籍或示例代码中学习BS4?BS3已经被抛弃了五年多了(甚至在BS3中,3.2也提供了BS4样式的名称以简化移植)。哦,我应该用什么来代替FindNext?实际上,我是通过看别人的脚本来学习BS的,所以…看看更新的脚本,而不是6年前的脚本。或者,最好看看文档中的例子。你的问题是什么@edyvedy13?您没有尝试获取这些标签吗?