0
votes

I am really an ETL guy trying to learn Python, please help

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

I am getting errors as

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

1
Isn't the error you get obvious?ozgur
Hi ozgur, i know very little basics of python .. can brief more please ..JDev

1 Answers

2
votes

Your error is coming from line 2:

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

Whatever url you're trying to access is returning a http error code

HTTP Error 405: Method Not Allowed

Looking at the urllib2 docs, you should only be using 1 url as an argument

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.

The 2nd argument you're putting in may be turning the request into a POST, which would explain the Method Not Allowed code.