urllib HTTP错误:400错误请求

时间:2015-01-06 03:59:20

标签: python post urllib http-error

我看了这个答案:Again urllib.error.HTTPError: HTTP Error 400: Bad Request因为它与我的问题非常相似,但是这个解决方案不起作用。我使用的是Python 3.3.2。像..something ..这些线条只是为了保护我的隐私而取代的价值观。它们应该是字符串

我收到错误400:来自此代码的错误请求:

import urllib.parse
import urllib.request

url = '..url..'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {'email':'..my email address..',
          'github':'..my github account..'}
headers = {'User-Agent':user_agent}
data = urllib.parse.urlencode(values)
data = data.encode('utf-8')
req = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(req) #this line causes the errors
page = response.read()

这是特定的错误消息:

Traceback (most recent call last):
  File "/Users/.../Documents/Code 2040/File.py", line 23, in <module>
    response = urllib.request.urlopen(req)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 156, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 475, in open
    response = meth(req, response)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 587, in http_response
    'http', request, response, code, msg, hdrs)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 513, in error
    return self._call_chain(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 447, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 595, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

0 个答案:

没有答案