Python 如何在BS4中搜索包含给定字符串的标记?
在BeautifulSoup4中,如何搜索文本包含特定字符串的标记?例如,在搜索“skyrim”时,我想打印包含字符串“skyrim”的每个标签的内容(例如游戏标题) 我试过使用Python 如何在BS4中搜索包含给定字符串的标记?,python,web-scraping,beautifulsoup,python-requests,Python,Web Scraping,Beautifulsoup,Python Requests,在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 st
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
预期结果:
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
实际结果:不打印任何内容您可以使用
汤。查找所有内容(string=re.compile(“您的字符串”)
获取文本,然后使用.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"))])
从bs4导入美化组
进口稀土
html=”“”
你好
你好
你好
"""
soup=BeautifulSoup(html,'html.parser')
打印([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>]
[你好
,你好
\n]
问题是子字符串检查,因为它区分大小写。如果使用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
尝试getText()方法。tag.getText()提及
steam\u结果的数据
。似乎与getText()重复会导致相同的错误。现在,它似乎很好。或者,建议进行不区分大小写的测试,如如果steam.getText().lower()中的'skyrim',lower():
,以便所有内容都以小写进行转换和比较。感谢您的建议。