Python:urllib2.HTTPError:HTTP错误401:authenticationrequired

时间:2016-03-15 07:46:17

标签: python urllib2

我试图获得一个网页,但遇到了这个问题。我查了一些参考文献,这就是我到目前为止所做的:

import sys
import urllib2
from bs4 import BeautifulSoup

user = 'myuserID'
password = "mypassword"

ip = sys.argv[1]
url = "http://www.websites.com/" + ip

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, user, password)
handler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)

header = {
    'Connection' : 'keep-alive',
    'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',
    'Accept-Language' : 'en-US,en;q=0.5',
    'Accept-Encoding' : 'gzip, deflate'
    }
html = urllib2.urlopen(urllib2.Request(url, None, header))
soup = BeautifulSoup(html, 'html.parser')
# some if else function afterwards #

当我尝试运行脚本时,它显示了这种错误:

python checker.py 8.8.8.8
Traceback (most recent call last):
  File "checker.py", line 34, in <module>
    html = urllib2.urlopen(urllib2.Request(url, None, header))
  File "C:\Python27\lib\urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 437, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 550, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 469, in error
    result = self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 656, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Python27\lib\urllib2.py", line 437, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 550, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 475, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 558, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: authenticationrequired

但是,如果我打开页面或其他网页,并手动输入我的凭据,那么此脚本可以正常工作。我错过了什么吗?

要添加,我目前的网络正在使用McAfee网关设备。因此,有时我们需要输入我们的凭证才能继续浏览网络。我们的用户/通行证与Active Directory集成在一起。这可能会导致问题吗?

1 个答案:

答案 0 :(得分:1)

这似乎工作得很好(取自另一个主题)

import urllib2
import base64
import sys

user = 'myuserID'
password = "mypassword"
ip = sys.argv[1]
url = "http://www.websites.com/" + ip
request = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (user, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)   
result = urllib2.urlopen(request)

或者您可以使用requests

from requests.auth import HTTPBasicAuth

user = 'myuserID'
password = "mypassword"
ip = sys.argv[1]
url = "http://www.websites.com/" + ip
res=requests.get(url , auth=HTTPBasicAuth(user, password))
print res.text