【发布时间】: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