如何绕过urllib2,Python中的错误304

时间:2010-09-26 13:05:40

标签: python urllib2

对于开场白,如果我尝试添加标题,则为opener = urllib2.build_opener():

request.add_header( '如果改性-因为',request.headers.get( '最后nodified'))

我收到错误代码:

Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    feeddata = opener.open(request)
  File "C:\Python27\lib\urllib2.py", line 391, in open
    response = self._open(req, data)
  File "C:\Python27\lib\urllib2.py", line 409, in _open
    '_open', req)
  File "C:\Python27\lib\urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 1173, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "C:\Python27\lib\urllib2.py", line 1142, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "C:\Python27\lib\httplib.py", line 946, in request
    self._send_request(method, url, body, headers)
  File "C:\Python27\lib\httplib.py", line 986, in _send_request
    self.putheader(hdr, value)
  File "C:\Python27\lib\httplib.py", line 924, in putheader
    str = '%s: %s' % (header, '\r\n\t'.join(values))
TypeError: sequence item 0: expected string, NoneType found

你如何解决这个问题?

我尝试从urllib2.BaseHandler构建一个类,但它不起作用。

1 个答案:

答案 0 :(得分:0)

您的追溯说明:expected string, NoneType found我从中推断出您已将None值存储为标头。你真的写'最后经过验证'吗?您所说的标题可能是“最后修改过的”,但即使这样,您应检查它是否存在,如果request.headers.get()返回“无”,则不会将其重新用作标题。