python - 从html中提取信息

标签 python html css

如何提取附加 html 的信息并将以下内容保存在文本文件中: Paragraph-ID\t TokenID\t TokenCoordinates\t TokenContent

因此,例如,第一行应该如下所示:

T102633 1 109,18,110,18 IV

T102634 1 527,29,139,16 Seit

...

我想使用 python。目前,我有以下内容:

root = lxml.html.parse('html-file').getroot()
tables = root.cssselect('table.main')
tables = root.xpath('//table[@class="main" and not(ancestor::table[@class="main"])]')


for elem in root.xpath("//span[@class='finereader']"):
    text = (elem.text or "") + (elem.tail or "")
        if elem.getprevious() is not None: # If there's a previous node
            previous = elem.getprevious()
            previous.tail = (previous.tail or "") + text # append to its tail
        else:
            parent = elem.getparent() # Otherwise use the parent
            parent.text = (parent.text or "") + text # and append to its text
        elem.getparent().remove(elem)

    txt = []

    txt += ([lxml.etree.tostring(t, method="html", encoding="utf-8") for t in tables])

    text = "\n".join(el for el in txt)
    output.write(text.decode("utf-8"))

这给了我这样的东西:

[:T102633-1 coord="109,18,110,18":]IV[:/T102633-1:]

现在,很明显我可以使用 string-find-method 来提取我想要的信息。但是就没有更优雅的解决方案了吗?使用“.attrib”或类似的东西? 感谢您的帮助!

在这里,可以找到 html:http://tinyurl.com/qjvsp4n

最佳答案

这段使用 BeautifulSoup 的代码给出了您感兴趣的所有 span:

from bs4 import BeautifulSoup

html_file = open('html_file')
soup = BeautifulSoup(html_file)

table = soup.find('table', attrs={'class':'main'})
# The first two tr's dont seem to contain the info you need, 
# so get rid of them
rows = table.find_all('tr')[2:] 
for row in rows:
    data = row.find_all('td')[1]
    span_element = data.find_all('span')
    for ele in span_element:
        print ele.text

获得格式为 [:T102639-3 coord="186,15,224,18":]L.[:/T102639-3:] 的数据后,使用 python regex 模块获取内容。

import re
pattern = re.compile('\[:(.*):\](.*)\[:\/(.*):\]')
data = "[:T102639-3 coord="186,15,224,18":]L.[:/T102639-3:]"
res = re.search(pattern, data)
# res.group(1).split()[0] then gives 'T102639-3'
# res.group(1).split()[1] gives coord="186,15,224,18"
# res.group(2) gives 'L.'

关于python - 从html中提取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22773843/

相关文章:

html - 使用 ng-repeat 时如何在对齐的行中显示复选框?

css - 仅突出显示文本,背景不透明

javascript - 使用 animate.css 重复动画循环

html - CSS 变量 : input:focus doesn't set variable

python - 为继承mixin类的类动态添加类变量

Python 和 SQL 批量插入

javascript - 操作网页中的部分 - 创建、扩展

python - Pyspark 将 JSON 读取为字典或结构而不是数据帧/RDD

python - 如何在 Windows 上安装 M2crypto

jquery - 无法使悬停处理程序适用于某些图像但不适用于所有图像