BeautifulSoup Search and Find a Text String in HTML/Page

import requests
from bs4 import beatifulsoup as bs
r = requests.get(url)
page = bs(r, 'html.parser')

for i in page.find_all(True):
        if word in i.text:
                    print(i + ' contains word')
            except TypeError:
                print('type error')

page.find_all(True) – returns all html tags in the beautifulsoup html page

if word in i.text: – check for a match in every tag’s text component, wrapped in try/fail in case a tag has no text component.

Leave your thoughts below. Follow on Twitter