How Do You Get Python to Search a Web Page for a Specific Pattern?

Problem scenario
How do you write a Python 3 program to parse the text of a web page and determine if a string is on the web page or not?

Change the "yoururl" variable assignment and the "searchterm" assignment below to what you desire. Then run the program below:

import re
import requests
yoururl = ''
searchterm = "learned"
r = requests.get(yoururl)
found =, r.text)
if found:
  print("The string/pattern '" + searchterm + "' was found when searching " + yoururl)
  print("The string/pattern '" + searchterm + "' was not found when searching " + yoururl)

Leave a comment

Your email address will not be published. Required fields are marked *