Python urllib2请求错误

时间:2015-06-05 00:30:05

标签: python urllib2

Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2
>>> req = urllib2.Request("http:///wp-login.php")
>>> website='kseek.com.my'
>>> req = urllib2.Request("http://"+website+"/wp-login.php")
>>> req.add_header('User-agent', 'Mozilla 5.10')
>>> req.add_header('Referer', 'http://'+website)
>>> data = urllib2.urlopen(req, timeout=6).read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 407, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 520, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 445, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 379, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 528, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 406: Not Acceptable
>>> req = urllib2.Request("http://"+website+"/")
>>> req.add_header('User-agent', 'Mozilla 5.10')
>>> req.add_header('Referer', 'http://'+website)
>>> data = urllib2.urlopen(req, timeout=6).read()
>>>

如你所见, 当请求/wp-login.php,我可以通过浏览器手动到达甚至卷曲我得到406错误 而使用相同的方法请求/index.php,工作没有问题 有什么帮助吗?

1 个答案:

答案 0 :(得分:4)

您已获得HTTP error 406,因为您错过了func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { // update any CLLocation values // And then call the method with the code that you need to execute ASAP self.yourMethod() } 标头。在打开URL之前添加以下内容:

Accept

<强>输出:

req.add_header('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8')