Home > Net >  Function in python to search for specific word in a text
Function in python to search for specific word in a text

Time:11-02

I would like to get know if the person studied maybe at another university. I just gave a random website

from bs4 import BeautifulSoup
 
import requests

url = 'https://www.liverpool.ac.uk/environmentalsciences/staff/daniel-arribas-bel/'

r = requests.get(url)

soup = BeautifulSoup(r.content,'lxml')
print(soup.find(text='About').find_next('div').get_text().strip())'

Now by print there is a text, but I would like to know if there is a function in python where you can give university and says, if it is in the text or not.

I search for a function which gave the output yes, if the word university is in the text.

I expect an output YES, NO, TRUE or FALSE

CodePudding user response:

Getting 404 for your url, think you have to be logged in


Simply check if university is in the scraped text:

if 'university' in soup.find(text='About').find_next('div').get_text(strip=True):
    print(True)
else:
    print(False)

Alternative:

if soup.find(text='About').find_next('div').get_text(strip=True).find('university') >= 0:
    print (True)
else:
    print(False)
  • Related