精确的文本匹配if语句python beautifulsoup

问题描述 投票:-1回答:2

我正在尝试使用以下代码找到“完全文本匹配”。该网站是: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标签中,因为它位于不同的网站上。

python beautifulsoup string-matching
2个回答
0
投票
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")

2
投票

有几种方法可以用BeautifulSoup搜索文本:

  • searching function。使用函数作为text值: results = soup.find_all(text=lambda text: text and 'offering at Giraffe' in text)
  • regular expression。使用正则表达式模式作为text值: import re results = soup.find_all(text=re.compile(r'offering at Giraffe'))
© www.soinside.com 2019 - 2024. All rights reserved.