python:urllib2.HTTPError:HTTP错误405:方法不允许

python: urllib2.HTTPError: HTTP Error 405: Method Not Allowed

我真的是一个试图学习Python的ETL人,请帮忙

1
2
3
4
5
6
7
8
import urllib2
urls =urllib2.urlopen("url1","url2")
i=0
while i< len(urls):
  htmlfile = urllib2.urlopen(urls[i])
  htmltext = htmlfile.read()
  print htmltext
  i+=1

我收到的错误是

Traceback (most recent call last):
File".\test.py", line 2, in
urls =urllib2.urlopen("url1","url2")
File"c:\python27\Lib\urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File"c:\python27\Lib\urllib2.py", line 437, in open
response = meth(req, response)
File"c:\python27\Lib\urllib2.py", line 550, in http_response
'http', request, response, code, msg, hdrs)
File"c:\python27\Lib\urllib2.py", line 475, in error
return self._call_chain(*args)
File"c:\python27\Lib\urllib2.py", line 409, in _call_chain
result = func(*args)
File"c:\python27\Lib\urllib2.py", line 558, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 405: Method Not Allowed


您的错误来自第2行:

urls =urllib2.urlopen("url1","url2")

无论您尝试访问哪个URL,都会返回http错误代码

HTTP Error 405: Method Not Allowed

查看urllib2文档,您应该只使用1个url作为参数

https://docs.python.org/2/library/urllib2.html

Open the URL url, which can be either a string or a Request object.

data may be a string specifying additional data to send to the server, or None if no such data is needed. Currently HTTP requests are the only ones that use data; the HTTP request will be a POST instead of a GET when the data parameter is provided.

您输入的第二个参数可能是将请求转换为POST,这将解释方法不允许的代码。