Skip to content

Instantly share code, notes, and snippets.

@grantglidewell
Last active December 4, 2018 18:06
Show Gist options
  • Save grantglidewell/8f6e8fb41847d7b468325abcb674de37 to your computer and use it in GitHub Desktop.
Save grantglidewell/8f6e8fb41847d7b468325abcb674de37 to your computer and use it in GitHub Desktop.
from urllib2 import urlopen
import time
def scrape(url, term) :
# Make the http request for the resource
response = urlopen(url)
# Read the response body to a variable
html = response.read()
# Create a unique token for filename
ts = time.time()
# Generate a string for the filename
filename = "scrape" + str(ts) + ".html"
# Write html as a file to disk
f = open(filename, "w")
f.write(html)
# Open the file
s = open(filename, "r")
# Read the file and check if the search term is present
if term in s.read():
# Return a message to the user that the search term was found
return "Found " + term + " in " + url
# Return a message to the user that the search term was not found
return term + " was not found in " + url
print scrape('URL', 'SearchTerm')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment