Fetch info from website and write to txt file.

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Mon Mar 6 18:14:58 EST 2006


Pitmairen a écrit :
> I want to make a program that get info from a website and prints it out
> in a txt file.
> 
> I made this:
> 
> import urllib
> f = urllib.urlopen("http://www.imdb.com/title/tt0407304/")
> s = f.read()
> k = open("test.txt","w")
> k.write(s)
> k.close()
> f.close()
> 
> That saves all the html code into the test.txt file. But if i for
> example only want the genre, plot outline and Cast overview to be
> written to the txt file. How can i do that?
> 

Seems like you want BeautifulSoup:
http://www.crummy.com/software/BeautifulSoup/


> And another problem i have:
> 
> If the txt file i want the information to be saved in already have some
> text saved in it. How can i save the info from the website  between the
> text that was there before?
> 
> for example:
> 
> blablablablablablablabla
> blablablablablablablabla
> blablablablablablablabla
> (inset info from website here)
> blablablablablablablabla
> blablablablablablablabla
> blablablablablablablabla
> 

You need to be able to identify the place where you want to insert your 
data. Then it's a matter of reading the original file, creating a temp 
file, writing lines before insertion point, writing data to insert, 
writing remaing lines, closing all files, replacing original file by the 
temp file.



More information about the Python-list mailing list