刮一张桌子然后写信给csv

时间:2014-04-01 15:06:38

标签: python beautifulsoup

我对蟒蛇和美丽的汤很新。这是我的第一个“真实”项目。我想从网站上抓一些信息。到目前为止,我已经半成功了。我已经确定了这个表并得到了python来很好地打印出相关信息。

我很难写信息python打印到可用的csv文件。

这是我的代码。让python打印我需要的信息。

for row in table_1.find_all('tr'):
    tds = row.find_all('td')
    try:
        a = str(tds[0].get_text())
        b = str(tds[1].get_text())
        c = str(tds[2].get_text())
        d = str(tds[3].get_text())
        e = str(tds[4].get_text())
        f = str(tds[5].get_text())
        g = str(tds[7].get_text())
        print 'User Name:' +  a
        print 'Source:' +  b
        print 'Staff:' +  c
        print 'Location:' +  d
        print 'Attended On:' +  e
        print 'Used:' +  f
        print 'Date:' +  g + '\n'


    except:
        print 'bad string'
        continue

2 个答案:

答案 0 :(得分:0)

以下是收集数据的更简洁方法:

columns = ["User Name", "Source", "Staff", "Location", "Attended On", "Used", "Date"]
table = []

for row in table_1.find_all('tr'):
    tds  = row.find_all('td')
    try:
        data = [td.get_text() for td in tds]
        for field,value in zip(columns, data):
            print("{}: {}".format(field, value))
        table.append(data)
    except:
        print("Bad string value")

然后你可以写为csv

import csv

with open("myfile.csv", "wb") as outf:                 # Python 2.x
# with open("myfile.csv", "w", newline="") as outf:    # Python 3.x
    outcsv = csv.writer(outf)

    # header row
    outcsv.writerow(columns)

    # data
    outcsv.writerows(table)

答案 1 :(得分:0)

对于循环的每次迭代,您可以将a通过g附加到列表中的列表中。然后使用:

my_list = []
for row in table_1.find_all('tr'):
    tds = row.find_all('td')
    a = str(tds[0].get_text())
    b = str(tds[1].get_text())
    c = str(tds[2].get_text())
    d = str(tds[3].get_text())
    e = str(tds[4].get_text())
    f = str(tds[5].get_text())
    g = str(tds[7].get_text())
    my_list.append([a,b,c,d,e,f,g])

然后:

import csv
with open('output_table.csv', 'wb') as csvfile:
    wr= csv.writer(csvfile,lineterminator = '\n')
    wr.writerows(my_list)