在函数中使用urllib2

1 投票
1 回答
4192 浏览
提问于 2025-04-17 12:05

当我在使用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 个回答

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)

撰写回答