0
我刚刚超过1500个html页面(1.html到1500.html)。我使用Beautiful Soup编写了一个代码,该代码提取了我需要的大部分数据,但是“遗漏”了表中的一些数据。将多个html文件中的文本拼凑为一个csv文件
我输入:如文件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文件,不幸的是我不知道如何处理我的代码以获取所需的所有数据!
Hi TheNIK,所有的html文件在格式方面都完全一样,唯一的区别是它们之间保存的tr标签 – Lulu 2011-01-11 14:27:48