Python: urllib2.HTTPError: HTTP Error 401: Unauthorized

I tried to load a webpage, but I ran into this problem. I have a username and password, but I don't know how to use them in python code. I looked at the python tutorial and here is what I wrote:

import urllib2 password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() username = 'user' password = 'pass' top_level_url = "www.something.com:80" password_mgr.add_password(None, top_level_url, username, password) handler = urllib2.HTTPBasicAuthHandler(password_mgr) opener = urllib2.build_opener(handler) opener.open('http://www.something.com/h/h.html') urllib2.install_opener(opener) response = urllib2.urlopen() page = response.read() print page 

Something is wrong?

+7
source share
2 answers

Here is the working code

 import urllib2 url = 'http://www.abc.com/index.html' username = 'user' password = 'pass' p = urllib2.HTTPPasswordMgrWithDefaultRealm() p.add_password(None, url, username, password) handler = urllib2.HTTPBasicAuthHandler(p) opener = urllib2.build_opener(handler) urllib2.install_opener(opener) page = urllib2.urlopen(url).read() 
+20
source

I think you can use a query module that will make it easier for you.

 import requests username = 'user' password = 'pass' url = 'http://www.example.com/index.html' r = requests.get(url, auth=(username, password)) page = r.content() print page 
+5
source

All Articles