AttributeError: 'HTTPResponse' object has no attribute 'type'

python, python-3.x

Solution

The problem is that you're trying to call `urlopen` on the result of `urlopen`.

Just call it once, like this:

nhl_site = urlopen('http://sports.yahoo.com/nhl/rss')
tree = ET.parse(nhl_site)

The error message probably could be nicer. If you look at the docs for `urlopen`:

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

Clearly the `http.client.HTTPResponse` object that it returns is neither a string nor a `Request` object. What's happened here is that `urlopen` sees that it's not a string, and therefore assumes it's a `Request`, and starts trying to access methods and attributes that `Request` objects have. This kind of design is generally a good thing, because it lets you pass things that act just like a `Request` and they'll just work… but it does mean that if you pass something that doesn't act like a `Request`, the error message can be mystifying.

Problem

So, I am trying to build a program that will retrieve the scores of the NHL's season through the use of yahoo's RSS feed. I am not an experienced programmer, so some things haven't quite gotten into my head just yet. However, here is my code so far: ``` from urllib.request import urlopen import xml.etree.cElementTree as ET YAHOO_NHL_URL = 'http://sports.yahoo.com/nhl/rss' def retrievalyahoo(): nhl_site = urlopen('http://sports.yahoo.com/nhl/rss') tree = ET.parse(urlopen(nhl_site)) retrievalyahoo() ``` The title above states the error I get after I test the aforementioned code. EDIT: Okay, after the fix, the traceback error comes as this, to which I am puzzled: ``` Traceback (most recent call last): File "C:/Nathaniel's Folder/Website Scores.py", line 12, in <module> retrievalyahoo() File "C:/Nathaniel's Folder/Website Scores.py", line 10, in retrievalyahoo tree = ET.parse(nhl_site) File "C:\Python33\lib\xml\etree\ElementTree.py", line 1242, in parse tree.parse(source, parser) File "C:\Python33\lib\xml\etree\ElementTree.py", line 1730, in parse self._root = parser._parse(source) File "<string>", line None xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 17, column 291 ```

Original source