vendredi 28 octobre 2016

Urllib : Page not found if http/https is not present in the webpage link

I have the code:

     def download_page(url):
                import urllib
                try:
                    headers = {}
                    headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17"
                    req = urllib2.Request(url, headers = headers)
                    response = urllib2.urlopen(req)
                    page = response.read()
                    return page    
                except:
                    return"Page Not found"

I have passed the url as www.quora.com but it returns page not found. Only working if I pass the url as http://www.quora.com or other link with http before. Secondly, webpage also doesn't download if I pass the url without www like quora.com. Is there any problem in my code or it is a bug in urllib? How to solve this issue?




Aucun commentaire:

Enregistrer un commentaire