0

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
    f.write(lines)
#f.writelines('data')
f.close()

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.

4
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by richieking
0

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:
    f.write(data)

Edited by snippsat: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.