python - 从 difflib 中获取更细粒度的差异(或者通过后处理差异来实现相同目的的方法)

标签 python beautifulsoup html-parsing lxml difflib

正在下载this页面并对其进行较小的编辑,将本段中的第一个 65 更改为 68:

enter image description here

然后,我使用 BeauifulSoup 解析两个源,并使用 difflib 区分它们。

url = 'https://secure.ssa.gov/apps10/reference.nsf/links/02092016062645AM'
response = urllib2.urlopen(url)
content = response.read()  # get response as list of lines

url2 = 'file:///Users/Pyderman/projects/temp/02092016062645AM-modified.html'
response2 = urllib2.urlopen(url2)
content2 = response2.read()  # get response as list of lines
import difflib
d = difflib.Differ()

diffed = d.compare(content, content)

soup = bs4.BeautifulSoup(content, "lxml")
soup2= bs4.BeautifulSoup(content2, "lxml")
diff = d.compare(list(soup.stripped_strings), list(soup2.stripped_strings))
changes = [change for change in diff if change.startswith('-') or  change.startswith('+')]
for change in changes:
    print change

打印更改会给出:

- The Achieving a Better Life Experience (ABLE) Act, H.R. 5771, legislation passed on December 19, 2014. It contains a Title II provision that changes the age at which workers compensation/public disability offset ends for disability beneficiaries from age 65 to full retirement age (FRA).  This provision will apply to any individual who attains age 65 on or after December 19, 2015 (the one year anniversary of enactment of this bill).  Two new Universal Text Identifiers (UTIs), UTI WCP060 and WCP061 were created to comply with this change.
+ The Achieving a Better Life Experience (ABLE) Act, H.R. 5771, legislation passed on December 19, 2014. It contains a Title II provision that changes the age at which workers compensation/public disability offset ends for disability beneficiaries from age 68 to full retirement age (FRA).  This provision will apply to any individual who attains age 65 on or after December 19, 2015 (the one year anniversary of enactment of this bill).  Two new Universal Text Identifiers (UTIs), UTI WCP060 and WCP061 were created to comply with this change.

因此,尽管变化很小,但它还是打印了整个段落。我认为它通过整个段落而不是句子来显示差异是一件好事,但是我们可以以某种方式使输出更加精细吗?就目前情况而言,如果我想突出显示仅更改的文本,我将必须对这两个几乎相同的字符串进行一些额外的增量比较。

最佳答案

您可以使用nltk.sent_tokenize()将汤字符串拆分成句子:

from nltk import sent_tokenize

sentences = [sentence for string in soup.stripped_strings for sentence in sent_tokenize(string)]
sentences2 = [sentence for string in soup2.stripped_strings for sentence in sent_tokenize(string)]

diff = d.compare(sentences, sentences2)
changes = [change for change in diff if change.startswith('-') or  change.startswith('+')]
for change in changes:
    print(change)

仅打印检测到更改的适当句子:

- It contains a Title II provision that changes the age at which workers compensation/public disability offset ends for disability beneficiaries from age 65 to full retirement age (FRA).
+ It contains a Title II provision that changes the age at which workers compensation/public disability offset ends for disability beneficiaries from age 68 to full retirement age (FRA).

关于python - 从 difflib 中获取更细粒度的差异(或者通过后处理差异来实现相同目的的方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35375004/

相关文章:

python - 如何在 JSON 中获取 PrimaryKeyRelatedField 的字符串表示形式

python - Python 可以打开的文件的最大大小?

python - Django 查询集 OR 语句

Python代码获取源页面中表格的html数据

python - 这个怎么解析呢?尝试使用 BeautifulSoup 和 Python 从非 HTML 网页提取数据

python - 从对象实例化内部的元组中提取值

python - 名称错误 : name 'bs4' is not defined

python - 在 Python 中将包含 html 标签的字符串拆分为其构建 block

python - 无法从 xpath python 获取值

html - 任何主流浏览器是否都有内置的 HTML 验证器?