【问题标题】:How can I remove specific value pairs from an inline style tag using python?如何使用 python 从内联样式标签中删除特定的值对?
【发布时间】:2019-08-29 05:50:50
【问题描述】:

我正在尝试解析一些具有一些讨厌的内联样式的 html。 它看起来像这样

<span class="text_line" data-complex="0" data-endposition="4:2:86:5:0" data-position="4:2:74:2:0" style="font-family: scala-sans-offc-pro--; width: 100%; word-spacing: -2.66667px; font-size: 24px !important; line-height: 40px; font-variant-ligatures: common-ligatures; display: block; height: 40px; margin-left: 75px; margin-right: 155px;">

我正在尝试仅删除属性值对 word-spacing: -2.66667px;。这里有几百条这样的线,没有两条是相同的。有时间距为word-spacing: -4px,有时为word-spacing: -3.78632px; 或其他随机数。

我尝试了美丽的汤,我想出了如何删除整个标签,这不是我想要的。我不知道如何用正则表达式来做到这一点。而且我读到最好避免尝试使用正则表达式编辑 HTML。

我的想法是使用漂亮的汤将所有跨度标签保存到变量中,然后使用string.find() 获取所有“w”在字间距中的索引,然后找到下一个半列。然后在我有一个列表之后,找到一种方法来切割这些索引处的字符串并将剩余部分重新连接在一起。也许在“;”处分裂更好......我现在不知道了。大脑又炸又累。 :P

    def __init__(self, first_index, last_index):
        self.first = first_index
        self.last = last_index
def getIndices(text, start_index):
    index = CutPointIndex(None, None)
    index.first = text.find("word-spacing", start_index, end_index)
    if(index.first != -1):
        index.last = text.find(";", index.first , end_index)
    return index

鉴于类似 style="font-family: scala-sans-offc-pro--; width: 100%; word-spacing: -3.71429px;"

style="font-family: scala-sans-offc-pro--; width: 100%; word-spacing: -5px;

或预期结果应该是值的任何其他变化 style="font-family: scala-sans-offc-pro--; width: 100%;

【问题讨论】:

  • 您的 html 看起来如何?你能分享一些样品吗?
  • @min2bro` 快速棕色 `
  • 预期输出是什么?您要提取此 word-spacing: -2.66667px 还是将 word-spacing: xxx px 替换为空字符串?
  • @min2bro 是的!正是!

标签: python regex string parsing beautifulsoup


【解决方案1】:

我猜你可能想要re.sub 变量word-spacing

import re

regex = r"\s*word-spacing\s*:\s*[^;]*;"

test_str = '''
style="font-family: scala-sans-offc-pro--; width: 100%; word-spacing: -3.71429px;"
style="font-family: scala-sans-offc-pro--; width: 100%; word-spacing: -5px;"
style="font-family: scala-sans-offc-pro--; width: 100%;"

'''

print(re.sub(regex, "", test_str))

输出

style="font-family: scala-sans-offc-pro--; width: 100%;"
style="font-family: scala-sans-offc-pro--; width: 100%;"
style="font-family: scala-sans-offc-pro--; width: 100%;"

如果您想探索/简化/修改表达式,它已经 在右上角的面板上进行了解释 regex101.com。如果你愿意,你 也可以在this link看,怎么搭配 针对一些样本输入。


【讨论】:

  • 那个好像能选中第一个表达式,能找到所有匹配的吗? regex101.com/r/9hQApv/1
  • 哦,太棒了,谢谢!好的,所以如果我想以编程方式设置全局标志。 regex = r"\sword-spacing\s:\s*[^;]*;"g 是正确的方法吗?
【解决方案2】:

您可以匹配具有该属性的元素并删除该部分。

我在; 上拆分样式属性(仅适用于相关标签),然后重新组合排除不需要的对

';'.join([i for i in t['style'].split(';') if 'word-spacing' not in i])

但您也可以轻松更新 word-spacing 的值

from bs4 import BeautifulSoup as bs

html = '''
<span class="text_line" data-complex="0" data-endposition="4:2:86:5:0" data-position="4:2:74:2:0" style="font-family: scala-sans-offc-pro--; width: 100%; word-spacing: -2.66667px; font-size: 24px !important; line-height: 40px; font-variant-ligatures: common-ligatures; display: block; height: 40px; margin-left: 75px; margin-right: 155px;">
'''
soup = bs(html, 'lxml')

for t in soup.select('[style*= word-spacing]'):
    t['style'] = ';'.join([i for i in t['style'].split(';') if 'word-spacing' not in i])
print(soup)

阅读:

  1. https://www.crummy.com/software/BeautifulSoup/bs4/doc/#attributes
  2. https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors

【讨论】:

  • 谢谢!我明天将阅读这些链接并尝试该解决方案。现在,我将 regex-replace 与我在 Visual Code Studio 中制作的批处理脚本一起使用。这使它成为一个两阶段的过程。我想让它成为纯 python 中的一个阶段过程。
猜你喜欢
  • 1970-01-01
  • 2013-03-25
  • 2016-08-31
  • 2012-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-05
相关资源
最近更新 更多