Write an entire html table to a text file
Question: I'm attempting to download a table from a site and bring it in to a table. I
can see the output in interpreter however when I write the text file it only
has one line. How do I write the entire table to a text?

    
    
    #!/usr/bin/env python
    from mechanize import Browser
    from bs4 import BeautifulSoup
    import urllib2,cookielib
    import time
    
    mech = Browser()
    mech.set_handle_robots(False)
    mech.set_handle_equiv(True)
    mech.set_handle_redirect(True)
    mech.set_handle_robots(False)
    mech.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
    
    url = "http://www.marinetraffic.com/en/ais/index/positions/all/shipid:415660/mmsi:354975000/shipname:ADESSA%20OCEAN%20KING/_:6012a2741fdfd2213679de8a23ab60d3"
    
    page = mech.open(url)
    #html = page.read()
    
    DateTime = time.strftime("%Y%m%d-%H%M")
    Month = time.strftime ("%mm-%Y")
    
    html = page.read()
    soup = BeautifulSoup(html)
    table = soup.find("table",attrs={"class":"table table-hover text-left"})
    
    
    for row in table.findAll('tr')[1:]:
        items = row.text.replace(u"kn","")# remove kn so items line up when unpacking
        time, ais_source, speed_km, lat, lon, course = items.split()[1:7]
        data = items.split()[1:7]
        text_file = open(DateTime + '.txt',"w")
        text_file.write(str(data))
        text_file.close()
        print items
    


Answer: You are opening the file on every pass through the loop, in mode "w" which
means write (i.e. overwrite whatever is currently in the file). I recommend
you open the file before the loop and close it after the loop. You could also
open it in append mode on every pass through the loop.

    
    
    with open(DateTime + '.txt',"w") as text_file:
        for row in table.findAll('tr')[1:]:
            items = row.text.replace(u"kn","")# remove kn so items line up when unpacking
            time, ais_source, speed_km, lat, lon, course = items.split()[1:7]
            data = items.split()[1:7]
            text_file.write(str(data))
    

