【问题标题】:scraping text from multiple html files into a single csv file将多个 html 文件中的文本抓取到单个 csv 文件中
【发布时间】:2011-01-11 14:00:41
【问题描述】:

我只有 1500 多个 html 页面(1.html 到 1500.html)。我使用 Beautiful Soup 编写了一个代码,该代码提取了我需要的大部分数据,但“遗漏”了表中的一些数据。

我的输入: 例如文件 1500.html

我的代码:

#!/usr/bin/env python
import glob
import codecs
from BeautifulSoup import BeautifulSoup
with codecs.open('dump2.csv', "w", encoding="utf-8") as csvfile:
for file in glob.glob('*html*'):
        print 'Processing', file
        soup = BeautifulSoup(open(file).read())
        rows = soup.findAll('tr')
        for tr in rows:
                cols = tr.findAll('td')
                #print >> csvfile,"#".join(col.string for col in cols)
                #print >> csvfile,"#".join(td.find(text=True))
                for col in cols:
                        print >> csvfile, col.string
                print >> csvfile, "==="
        print >> csvfile, "***"

输出:

一个 CSV 文件,包含 1500 行文本和数据列。由于某种原因,我的代码没有提取所有必需的数据,而是“遗漏”了一些数据,例如表开头的地址 1 和地址 2 数据没有出现。我修改了代码以放入 * 和 === 分隔符,然后我使用 perl 放入一个干净的 csv 文件,不幸的是我不确定如何使用我的代码来获取我正在寻找的所有数据!

【问题讨论】:

    标签: html-parsing beautifulsoup


    【解决方案1】:

    查找丢失参数的文件, 然后尝试分析发生了什么......

    我认为相同的文件有不同的格式,或者可能错过了真正的地址。

    【讨论】:

    • 嗨 TheNIK,所有的 html 文件在格式方面都完全相同,唯一的区别是它们在 tr 标签之间保存的数据
    猜你喜欢
    • 2010-10-29
    • 2021-06-02
    • 2016-05-24
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多