Created
March 31, 2012 21:22
-
-
Save gnufs/2268656 to your computer and use it in GitHub Desktop.
A rudimentary web scraper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, requests | |
from bs4 import BeautifulSoup | |
def scrape(url='http://example.com'): | |
try: | |
html = requests.get(url).content | |
except: | |
print "URL doesn\'t load" | |
exit() | |
page = BeautifulSoup(html) | |
try: | |
bodytext = page.body.findAll(text=True) | |
for s in bodytext: | |
print s | |
except: | |
print "Page could not be read." | |
if len(sys.argv) != 2: | |
url = raw_input("Please enter a URL: ") | |
else: | |
url = sys.argv[1] | |
if url.lower().startswith('http'): | |
scrape(url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment