How can I export data from a website and write the contents to a text file?

ryguy7272 ryanshuell at gmail.com
Wed Nov 18 12:03:58 EST 2015


On Wednesday, November 18, 2015 at 11:58:17 AM UTC-5, Chris Angelico wrote:
> On Thu, Nov 19, 2015 at 3:37 AM, ryguy7272 <ryanshuell at gmail.com> wrote:
> >       text_file = open("C:/Users/rshuell001/Desktop/excel/Text1.txt", "wb")
> >         z = str(link)
> >         text_file.write(z + "\n")
> >         text_file.write("\n")
> >         text_file.close()
> 
> You're opening the file every time you go through the loop,
> overwriting each time. Instead, open the file once, then start the
> loop, and then close it at the end. You can use a 'with' statement to
> do the closing for you, or you can do it the way you are here.
> 
> ChrisA



Thanks.  What would the code look like?  I tried the code below, and got the same results.


for item in soup.find_all(class_='lister-list'):
    for link in item.find_all('a'):
        #print(link)
        z = str(link)
        text_file = open("C:/Users/rshuell001/Desktop/excel/Text1.txt", "wb")
        text_file.write(z + "\n")
        text_file.close()






More information about the Python-list mailing list