我正在尝试使用以下代码找到“完全文本匹配”。该网站是:https://www.girafferestaurant.co.nz/menu。当我打印(soup.find_all(text = True))我可以返回并搜索文本,但我只想匹配或不匹配,这取决于单词/短语(在这种情况下'在Giraffe提供')是否在声明。
以下是我尝试过的。
text = soup.find_all(text=True)
if 'offering at Giraffe' in text:
print ("Match")
else:
print ("No Match")
另外,我使用了text = soup.find_all('p'),但文本并不总是在p标签中,因为它位于不同的网站上。
import bs4
import requests
url = 'https://www.girafferestaurant.co.nz/menu'
r = requests.get(url)
soup = bs4.BeautifulSoup(r.text,'html.parser')
text = soup.find_all(text=True)
matches = []
for item in text:
if 'offering at Giraffe' in item:
matches.append(item)
if matches != []:
print ('Match')
else:
print ("No Match")
编辑:为您的后续行动。如果您只想查看整个文本:
import bs4
import requests
url = 'https://www.girafferestaurant.co.nz/menu'
r = requests.get(url)
soup = bs4.BeautifulSoup(r.text,'html.parser')
text = soup.text
matches = []
if 'offering at Giraffe' in text and 'customised set' not in text:
matches.append(text)
if matches != []:
print ('Match')
else:
print ("No Match")
有几种方法可以用BeautifulSoup
搜索文本:
text
值:
results = soup.find_all(text=lambda text: text and 'offering at Giraffe' in text)
text
值:
import re
results = soup.find_all(text=re.compile(r'offering at Giraffe'))