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?

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

import re
import requests
yoururl = 'https://www.continualintegration.com/'
searchterm = "learned"
r = requests.get(yoururl)
found = re.search(searchterm, r.text)
if found:
  print("The string/pattern '" + searchterm + "' was found when searching " + yoururl)
# found.group(0))
else:
  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 *