如何在BS4中搜索包含给定字符串的标记?

2024-05-19 08:11:52 发布

您现在位置:Python中文网/ 问答频道 /正文

在BeautifulSoup4中,如何搜索包含特定字符串的文本标记?例如,在搜索“skyrim”时,我想打印包含字符串“skyrim”的每个标签的内容(例如游戏标题)。你知道吗

我试过用

    if 'skyrim' in tag.string:

但它从不打印任何东西。你知道吗

完整定义:

def search(self):
    steam_results = self.soup.find_all('span', class_='title')

    itr = 1
    for tag in steam_results:
        if self.title in tag.string:  # <--- Not working
            print(str(itr) + ': ' + tag.string + '\n')
            itr = itr + 1

steam_results样本:

>>> steam_results
[<span class="title">The Elder Scrolls V: Skyrim Special Edition</span>,
 <span class="title">Skyrim Script Extender (SKSE)</span>, 
 <span class="title">Enderal</span>, ...]

预期结果:

  1. 老卷轴V:Skyrim特别版
  2. Skyrim脚本扩展程序(SKSE)

实际结果:不打印任何内容


Tags: 字符串inself内容stringiftitletag
2条回答

问题是子字符串检查,因为它是case-sensitive。如果使用skyrim进行检查,将得到空结果,因为没有title包含skyrim,而是包含Skyrim。所以,把它和小写的标题比较一下

steam_results = soup.find_all('span', class_='title')
for steam in steam_results:
    if 'skyrim' in steam.getText().lower():
        print(steam.getText())

输出:

The Elder Scrolls V: Skyrim Special Edition
The Elder Scrolls V: Skyrim VR
Skyrim Script Extender (SKSE)
The Elder Scrolls V: Skyrim Special Edition - Creation Club

可以使用soup.find_all(string=re.compile("your_string_here")获取文本,然后使用.parent获取标记。你知道吗

from bs4 import BeautifulSoup
import re
html="""
<p id="1">Hi there</p>
<p id="2">hello<p>
<p id="2">hello there<p>
"""
soup=BeautifulSoup(html,'html.parser')
print([tag.parent for tag in soup.find_all(string=re.compile("there"))])

输出

[<p id="1">Hi there</p>, <p id="2">hello there<p>\n</p></p>]

相关问题 更多 >

    热门问题