首页 > 解决方案 > 在美丽的汤中将检索到的数据保存到 CSV

问题描述

for row in page_soup.find("table",{"class":"table"}).tbody.findAll('tr'):

    first_column = row.findAll('td')[0].contents

    second_column = row.findAll('td')[1].contents

    third_column = row.findAll('td')[2].contents

    print(first_column, second_column, third_column)

标签: javapythoncsvbeautifulsoup

解决方案


你可以从简单的事情开始:

for row in page_soup.find("table",{"class":"table"}).tbody.findAll('tr'):
    first_column = row.findAll('td')[0].contents
    second_column = row.findAll('td')[1].contents
    third_column = row.findAll('td')[2].contents

    print("\"" + first_column + "\",", "\"" + second_column + "\",", "\"" + third_column + "\"")

并将输出重定向到文件中。但是,这假定每列都有一个可能正确也可能不正确的字符串。如果列类型是数字,则删除转义的引号。


推荐阅读