Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的Simple.html过滤器-仅修改文本元素_Python_Html_Filter - Fatal编程技术网

python中的Simple.html过滤器-仅修改文本元素

python中的Simple.html过滤器-仅修改文本元素,python,html,filter,Python,Html,Filter,我需要过滤一组相当长但非常规则的.html文件,只有当它们出现在文本元素中时才修改一些结构 一个很好的例子是改变很难找到他好的一面!他没有 很难找到他的“好",;一边他没有 我可以很容易地用html.parser解析我的文件,但不清楚如何生成结果文件,它应该尽可能类似于输入,而无需重新格式化 我看了一眼漂亮的汤,但它看起来真的太大了,不适合这个?简单的任务 注意:我不需要/不想向任何类型的浏览器提供.html文件;我只需要他们更新可能的地方略有改变的内容 更新: 在@soundst

我需要过滤一组相当长但非常规则的.html文件,只有当它们出现在文本元素中时才修改一些结构

一个很好的例子是改变很难找到他好的一面!他没有

很难找到他的“好",;一边他没有

我可以很容易地用html.parser解析我的文件,但不清楚如何生成结果文件,它应该尽可能类似于输入,而无需重新格式化

我看了一眼漂亮的汤,但它看起来真的太大了,不适合这个?简单的任务

注意:我不需要/不想向任何类型的浏览器提供.html文件;我只需要他们更新可能的地方略有改变的内容

更新:

在@soundstripe advice之后,我编写了以下代码:

import bs4
from re import sub

def handle_html(html):
    sp = bs4.BeautifulSoup(html, features='html.parser')
    for e in list(sp.strings):
        s = sub(r'"([^"]+)"', r'“\1”', e)
        if s != e:
            e.replace_with(s)
    return str(sp).encode()

raw = b"""<p><div class="speech">it's hard to "find" his "good" side! He has <i>none</i>!<div></p>"""
new = handle_html(raw)
print(raw)
print(new)
不幸的是,BeautifulSoup试图从自身和我自己的利益出发变得过于聪明:

b'<p><div class="speech">it\'s hard to "find" his "good" side! He has <i>none</i>!<div></p>'
b'<p><div class="speech">it\'s hard to &amp;ldquo;find&amp;rdquo; his &amp;ldquo;good&amp;rdquo; side! He has <i>none</i>!<div></div></div></p>'
i、 e:它转换为普通&到&;因此,打破了&ldquo;请注意,我使用的是ByteArray,而不是字符串。相关吗


我怎样才能解决这个问题?

我不知道你为什么不使用BeautifulSoup。下面是一个例子,它会像你所问的那样替换你的引用

import re
import bs4

raw = b"""<p><div class="speech">it's hard to find his "good" side! He has <i>none</i>!<div></p> to <p><div class="speech">it's hard to find his &ldquo;good&rdquo; side! He has <i>none</i>!<div></p>"""
soup = bs4.BeautifulSoup(raw, features='html.parser')

def replace_quotes(s):
    return re.sub(r'"([^"]+)"', r'&ldquo;\1&rdquo;', e)


for e in list(soup.strings):
    # wrapping the new string in BeautifulSoup() call to correctly parse entities
    new_string = bs4.BeautifulSoup(replace_quotes(e))
    e.replace_with(new_string)

# use the soup.encode() formatter keyword to specify you want html entities in your output
new = soup.encode(formatter='html')


print(raw)
print(new)

您可以将SeleniumWebDriver用于that@Code_Ninja当前位置乍一看,它看起来比漂亮的汤更有用处。我错过了什么吗?哈哈,不要害怕API,selenium webdriver提供的功能比beautiful soup还多,因为它的主要创建目的是跟踪和自动化网站前端的更改。请查看更新的问题;我们快到了。。。但不完全是这样。通常你需要打开另一篇文章,先做一些研究,然后再问一个不同的问题,但我今天感觉很好:PI会指出,这个re.sub模式只适用于单个HTML字符串中匹配的引号对。您可能需要更像Word用于智能引号的功能—如果引号后面跟一个字母,则应该是左引号。如果后面有空格或标点符号,应该是正确的引语。治愈比疾病更糟糕。使用bs4.BeautifulSoupreplace“将字符串包装在…

”;外。。。被替换为删除,但是,..

保留并破坏格式化。我会接受你的回答,因为我只是简单地改变了它;“因为我并不真正关心最终产品中的html实体。谢谢。我知道re.sub.的局限性,但我仍然认为这是最好的选择;我的一个实际代码片段是:«” 他是一个成功的人。他是一个成功的人。他是一个成功的人。他是一个成功的人。他是一个成功的人! »« 安迪阿莫是一座观景楼吗? »不太清楚如何处理结束语引号,因为它被标点符号包围,这只是第一次成功。欢迎有任何见解。