1

I have the following code that returns the title of webpage

def get_title(url):
    page = urllib2.urlopen(url)
    p = lxml.html.parse(page)
    return p.find(".//title").text

1) Is this the most efficient method of finding a pages title (even after redirects)

2) How can I account for all the various exceptions that may occur (i.e. HTTPErrors, URLError, etc). I dont want to just use except Exception as e to catchall. I want to have specific exceptions

Bijan
  • 7,737
  • 18
  • 89
  • 149
  • 2
    Just look at the documentation for each function. You'd see [How to list all exceptions a function could raise in Python 3?](http://stackoverflow.com/q/32560116/2301450) and related posts. – vaultah Oct 06 '15 at 17:07
  • 1
    Not sure you need it but you could consider caching the results for each url in a memcached/redis instance so that you don't have to load the url every time. – JCOC611 Oct 06 '15 at 17:15

0 Answers0