python - ElementTree 错误,html 文件无法使用 Python/Sublime 进行解析

标签 python html parsing sublimetext2

我正在尝试解析几千个 html 文件并将变量转储到 csv 文件(excel 电子表格)中。我遇到了几个障碍,但第一个是:我无法让它正确解析文件。下面是简要说明、python 代码和回溯信息。

使用 Python 和 Sublime 解析 html 文件,我遇到了几个错误。正在运行的内容:它运行良好,直到if '.html' in file:。它不执行该循环。它将很好地迭代 print allFiles 。它还创建 csv 文件并创建标题(虽然不在单独的列中,但我可以稍后询问)。

问题似乎出在 if tree = ET.parse(HTML_PATH+"/"+file) 部分。我已经用几种不同的方式编写了这个(例如,没有“/”和/或"file")——到目前为止我还没有解决这个问题。

如果我可以提供更多信息,或者有人可以指导我查看其他文档,我将不胜感激。到目前为止,我还没有找到任何可以解决这个问题的东西。

非常感谢您的想法。

//C

# Parses out data from crawled html files under "html files"
# and places the output in output.csv.

import xml.etree.ElementTree as ET
import csv, codecs, os
from cStringIO import StringIO
# Note: you need to download and install this..
import unicodecsv

 # TODO: make into command line params (instead of constant)
CSV_FILE='output.csv'
HTML_PATH='/Users/C/data/Folder_NS'
f = open(CSV_FILE, 'wb')
w = unicodecsv.writer(f, encoding='utf-8', delimiter=';')
w.writerow(['file', 'category', 'about', 'title', 'subtitle', 'date', 'bodyarticle'])

# redundant declarations:
category=''
about=''
title=''
subtitle=''
date=''
bodyarticle=''
print "headers created"

allFiles = os.listdir(HTML_PATH)
#with open(CSV_FILE, 'wb') as csvfile:
print "all defined"

for file in allFiles:
    #print allFiles
    if '.html' in file:
        print "in html loop"
        tree = ET.parse(HTML_PATH+"/"+file)
        print '===================='
        print 'Parsing file: '+file
        print '===================='
        for node in tree.iter():
            print "tbody"
            # The tbody attribute spells it all (or does it):
            name = node.attrib.get('/html/body/center/table/tbody/tr/td/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]/table/tbody/tr[1]/td[1]/font')

            # Check common header stuff
            if name=='/html/body/center/table/tbody/tr/td/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]/table/tbody/tr[1]/td[1]/font':
                #print '    ------------------'
                #print '  Category:'
                category=node.text
                print "category"

f.close()

回溯:

文件“/Users/C/data/Folder_NS/data_parse.py”,第 34 行,位于 树 = ET.parse(HTML_PATH+"/"+文件) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py”,第 1182 行,解析中 树.parse(源,解析器) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py”,第 656 行,解析中 解析器.feed(数据) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py”,第 1642 行,提要中 self._raiseerror(v) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py”,第 1506 行,位于 _raiseerror 中 引发错误 xml.etree.ElementTree.ParseError:标签不匹配:第 63 行,第 2 列

最佳答案

您正在尝试使用 XML 解析器解析 HTML,而有效的 HTML 并不总是有效的 XML。您最好使用 lxml 中的 HTML 解析库。包。

import xml.etree.ElementTree as ET
# ...
tree = ET.parse(HTML_PATH + '/' + file)

将更改为

import lxml.html
# ...
tree = lxml.html.parse(HTML_PATH + '/' + file)

关于python - ElementTree 错误,html 文件无法使用 Python/Sublime 进行解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30721588/

相关文章:

jquery - 由 jquery 控制的多级 html 菜单

python - 解析 Python subprocess.check_output()

python - 如何分配 tf.placeholder?

python - 解释器维护的整数缓存有什么用?

python - 在 Python 中获取最后一个 '/' 或 '\\' 字符

javascript - jQuery - 如果页面上存在 DIV,则将 CSS 添加到其他 DIV

python pip安装scipy报错

javascript - Windows Phone 8(而非 8.1)在 Visual Studio 2013 中进行游戏

c - 解析对角双数组

c++ - 如何解析嵌套数组?