In this example, we'll update our HTML spell-checker so that we can check pages that are already being served, without requiring local access to the file system. To do this, we'll make use of the Python urllib2
module.
We'll be using
html_spelling.py
file as our base here, so create a copy of it and name the filehtml_spelling-b.py
.At the top of the file, update your import statements to include
urllib2
, and remove thefileinput
module as we'll not take advantage of it in this example.import urllib2 import enchant import optparse
Now, we'll update our module-level main code and add an option to accept a URL on the command-line.
if __name__ == '__main__': parser = optparse.OptionParser() parser.add_option('-u', '--url', help="URL to Check") opts, args = parser.parse_args() if not opts.url: parser.error("URL is required")
Finally, change the
fileinput.input
call to referenceurllib2.urlopen
, add a line number counter, and...