【问题标题】:Find with text using python and regex BeautifulSoup lxml使用 python 和正则表达式 BeautifulSoup lxml 查找文本
【发布时间】:2021-12-24 07:10:48
【问题描述】:

如何查找带有文本的数据? 例如

<td>
    <span class="data-list clear-none">active</span>
    <span class="data-list clear-none">not active</span>
    <span class="data-list clear-none">null</span>
    <span class="data-list clear-none">none</span>
<td>

我只想获取包含 active 的类?什么是最好的方法? 现在试试

current_lifter = soup.findAll("span", {"class": "data-list clear-none"})

但只想获取开始 active

的文本

【问题讨论】:

    标签: python python-3.x regex beautifulsoup python-requests


    【解决方案1】:

    使用string='active' 作为find(或findAll)的参数

    >>> soup.find("span", {"class": "data-list clear-none"}, string='active')
    <span class="data-list clear-none">active</span>
    

    是否有任何功能,例如 active *.我的意思是在活动后得到一切

    import re
    
    soup.find("span", {"class": "data-list clear-none"}, string=re.compile('^active.*'))
    

    请参阅documentationstring 也可以接受正则表达式。

    【讨论】:

    • 太好了,是否有任何功能,例如 active * 。我的意思是在活动后得到一切
    【解决方案2】:
    current_lifter = soup.find_all("span", {class = "data-list clear-none"}, string='active')
    

    然后运行迭代器获取文本。

    【讨论】:

    • 太好了,是否有任何功能,例如 active * 。我的意思是在活动后得到一切
    猜你喜欢
    • 2014-11-09
    • 2014-12-10
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 2012-09-01
    • 2018-07-17
    • 1970-01-01
    相关资源
    最近更新 更多