ABSP, Ch 11, Practice Project #4

My solution to the Practice Project #4 in Chapter 11 of the excellent book “Automate the Boring Stuff with Python” (ABSP)

#! usr/bin/env python3
# by lorenzo - 06/10/2017
# link verification - given the URL of a web page,
# attempts to download every linked page on the web page

import requests, bs4

print("Enter URL:")
myURL = input()

if not myURL.startswith("http"):
    myURL = "http://" + myURL

res = requests.get(myURL)
try:
    res.raise_for_status()
except Exception as exc:
    print("An error occurred: %s" % (exc))

mySoup = bs4.BeautifulSoup(res.text, "html.parser")
myLinks = mySoup.select("a[href]")
for i in range(len(myLinks)):
    res = requests.get(myURL)
    try:
        res.raise_for_status()
    except Exception as exc:
        print("An error occurred: %s" % (exc))

Comments about the code are welcome.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s