精确文本匹配if语句python beautifulsoup

时间:2018-12-12 22:00:42

标签: python beautifulsoup string-matching

我正在尝试使用以下代码查找“完全匹配的文字”。该网站是:https://www.girafferestaurant.co.nz/menu。当我打印(soup.find_all(text = True))时,我可以取回文本并进行搜索,但是我只想匹配还是不匹配,具体取决于单词/词组(在这种情况下为“在长颈鹿提供”)。声明。

以下是我尝试过的内容。

text = soup.find_all(text=True)
if 'offering at Giraffe' in text:
     print ("Match")
else: 
     print ("No Match")

此外,我使用了text = soup.find_all('p'),但文本并不总是位于p标签中,因为它位于不同的站点。

2 个答案:

答案 0 :(得分: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'))
    

答案 1 :(得分: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")
相关问题