hey all,
i'm trying to get all content of a webpage using python and trying to put them all into a file.

Here is my code:

import urllib
data = urllib.urlopen('http://www.site.net/index.html').read()
#print data     //if i ignore the rest part of this code the code is just working awesome...its printing the content of index.html
f = open('somefile.html','w')   //that i have already created.
for lines in data.read():
    print lines

I'm getting following errors:

Traceback (most recent call last):
  File "C:/Python24/showpage.py", line 5, in -toplevel-
    for lines in data.read():
AttributeError: 'str' object has no attribute 'read'

i'm using windows and Python 2.4.

Recommended Answers

All 4 Replies

your line 2 has a trailing read() that should be removed before you try to do line 5

Ohk...i have got that....thanks a lot for your help....

read() read the whole file into a string,so no need for lines in data
with open() close the file object auto.
So you can write it like this.

import urllib
data = urllib.urlopen('http://www.site.net/index.html').read()

with open('somefile.html', 'w') as f:

spot on snippsat

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.