Home > database >  Can't find string in Beautiful Soup [duplicate]
Can't find string in Beautiful Soup [duplicate]

Time:10-07

I wrote a very simple program to find phrases in text. It works fine when I search for a class or ID. But I can't get it to find specific text. In this example, I want it to see if the website python.org has the word 'python' anywhere on the page.

Oddly it is returning 'python not found'. Appreciate your help with this!

from bs4 import BeautifulSoup


response = requests.get("https://www.python.org/")

soup = BeautifulSoup(response.text, "lxml")

find_python = soup.find_all(string="python")


if find_python:
    print('found python')
else:
    print("python not found")

CodePudding user response:

The reason you're not getting any results back is because there isn't any element that simply contains the string python on that webpage. There are elements that contain the string Python, however, and you can see that by running:

soup.find(string="Python")
>>>'Python'

If you want to check whether the Python homepage contains the word 'python', which of course it will, you can do:

'python' in str(soup.html)
>>> True
  • Related