我希望得到可靠的差异,仅内容(结构变化很少,因此可以忽略)的this页。更具体地说,我需要做的唯一更改是添加了一个新的指令ID:
为了了解difflib将产生什么,我首先区分两个相同的HTML内容,希望什么也得不到:
url = 'https://secure.ssa.gov/apps10/reference.nsf/instructiontypecode!openview&restricttocategory=POMT'
response = urllib.urlopen(url
content = response.read()
import difflib
d = difflib.Differ()
diffed = d.compare(content, content)
由于difflib模仿UNIX diff
实用程序,所以我希望diffed
不包含任何内容(或者给出一些序列相同的指示,但是如果我'\n'.join
diffed
,我得到something resembling HTML(尽管它不在浏览器中呈现)
事实上,如果我用最简单的例子来区分两个字符:
diffed
=d.compare('a','a')
diffed.next()
生成以下内容:
所以我要么期待着difflib中的一些东西,它不能或不会提供(我应该改变策略),要么是我误用了它?有什么可行的方法来区分HTML?在
^{} 的参数应该是字符串序列。如果使用两个字符串,它们将被视为序列,因此逐个字符进行比较。在
所以你的例子应该改写为:
如果您只想比较html文件的内容,您可能应该使用解析器来处理它,并且只获取不带标记的文本,例如使用beauthulsoup的soup.stripped_strings:
^{pr2}$相关问题 更多 >
编程相关推荐