谁能告诉我如何使用 selenium 来搜索忽略撇号的东西?例如,如果我搜索单词“Hes”,并且页面内容中包含“He's”,它会找到它吗?
谢谢
使用 Selenium 忽略撇号,您可以使用正则表达式 (regex) 来匹配您要搜索的单词。在您的情况下,搜索“Hes”也应该匹配“He's”。from selenium import webdriver
from selenium import webdriver as dr
import re
dr =webdriver.Chrome()
dr.get('stackoverflow.com')
#SERACH IF THE WORD HAS REGEX
word_to_search = "Hes"
regex_pattern = re.compile(r'\b{}\b'.format(word_to_search), re.IGNORECASE)
matches = regex_pattern.findall(page_content)
这里,
:单词边界,确保该单词是一个完整的单词而不是另一个单词的一部分。 {}:您要搜索的单词的占位符。 re.IGNORECASE:忽略大小写,因此“Hes”将匹配“hes”、“HES”、“He's”等
# Check if the word was found:
if matches: print(f"Word '{word_to_search}' found.")
else: print(f"Word '{word_to_search}' not found.")