在函数中使用urllib2

时间:2012-02-01 04:35:57

标签: python function urllib2

当我使用urllib2模块而不是函数时,一切正常(这里是代码):

headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7'}
request = urllib2.Request(url, '', headers)
response = urllib2.urlopen(request)
returned_array = [response.geturl(), response.read()]
print returned_array

但是当我把它用于运作时:

def nk_get_site(url):
  headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7'}
  request = urllib2.Request(url, '', headers)
  response = urllib2.urlopen(request)
  returned_array = [response.geturl(), response.read()]
  return returned_array

它不起作用,错误:

Traceback (most recent call last):
File "C:\py\NetKit\netkit.py", line 37, in <module>
print nk_get_site('http://www.google.com/')
File "C:\py\NetKit\netkit.py", line 33, in nk_get_site
response = urllib2.urlopen(request)
File "C:\Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 400, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 438, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 372, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 521, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 405: Method Not Allowed

1 个答案:

答案 0 :(得分:5)

这与您将代码放入函数中无关,而与您正在测试的URL有关:

>>> headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7'}
>>> request = urllib2.Request(url, '', headers)
>>> response = urllib2.urlopen(request)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python25\lib\urllib2.py", line 124, in urlopen
    return _opener.open(url, data)
  File "C:\Python25\lib\urllib2.py", line 387, in open
    response = meth(req, response)
  File "C:\Python25\lib\urllib2.py", line 498, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python25\lib\urllib2.py", line 425, in error
    return self._call_chain(*args)
  File "C:\Python25\lib\urllib2.py", line 360, in _call_chain
    result = func(*args)
  File "C:\Python25\lib\urllib2.py", line 506, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 405: Method Not Allowed

通过将''传递给data参数,您将其转换为POST请求。如果您只想在GET请求上传递标题,则应使用以下内容:

request = urllib2.Request(url, headers=headers)