Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Function in python to search for specific word in a text

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I expect an output YES, NO, TRUE or FALSE

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading