Ozh
Ozh

Reputation: 729

Why is my very simple Python script failing?

noob.py here. I'm trying to fetch content from a page but the print statement raises an error I don't understand.

Actual code:

import urllib2
import sys

url = "http://make.wordpress.org/core/page/2/"
response = urllib2.urlopen(url)
html = response.read
print html

The output:

$ python get.py
<bound method _fileobject.read of <socket._fileobject object at 0x3722ec9a8d0>>

I suspect there is something Python doesn't like about that particular URL because it works with, say, http://www.python.org instead, but I can get any useful info to understand that.

What I don't get either is that, if I enclose this within a try: and except:↵ pass, I still get that error message.

Any pointers much liked.

Upvotes: 1

Views: 190

Answers (2)

Arovit
Arovit

Reputation: 3709

Good thing to learn here is that everything (functions, data, variables) in python is an object and print <obj> will give you something like object at 0x3722ec9a8d0>

Upvotes: 1

user2555451
user2555451

Reputation:

That's not an error; it is a string representation of the read method.

You are seeing it because on this line:

html = response.read

you forgot to invoke the read method. So, html is assigned to the method itself, not its return value.

Adding () after the method name will invoke it:

html = response.read()

Now, html is set to the return value of the read method.

Upvotes: 5

Related Questions