将整个html表写入文本文件

时间:2015-02-02 21:50:13

标签: python

我正在尝试从网站下载表格并将其带入表格。我可以在解释器中看到输出但是当我写文本文件时它只有一行。如何将整个表格写入文本?

#!/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

2 个答案:

答案 0 :(得分:1)

您在每次循环中打开文件,在模式“w”中表示写入(即覆盖当前文件中的任何内容)。我建议你在循环之前打开文件,然后在循环之后关闭它。你也可以在循环的每次传递中以追加模式打开它。

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))

答案 1 :(得分:1)

with open(DateTime + '.txt',"a") 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))
        print items

以附加模式打开文件,否则你会在每个循环中覆盖文件的内容,从而丢失先前写入的数据。