如何使用python从内联样式标记中移除特定的值对?

2024-09-27 00:14:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图解析一些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;或其他一些随机数。你知道吗

我尝了一口漂亮的汤,我想出了如何去掉整个标签,这不是我想要的。我不知道如何使用正则表达式。我读到最好避免用regex编辑HTML。你知道吗

我的想法是,使用beautiful soup将所有span标记保存到一个变量中,然后使用string.find()获得单词间距中所有“w”的索引,然后找到下一个半列。然后在我有了一个列表之后,找到一种方法,在这些索引处剪切字符串,并将剩余部分重新连接在一起。也许在“;”处分手更好。。。我现在不知道了。大脑是一个油炸和疲劳。:页

    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%;


Tags: textindexstylewidthfamilyprowordfirst
2条回答

您可以匹配具有该属性的元素并移除该部分。你知道吗

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

';'.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

我猜您可能想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%;"

If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


相关问题 更多 >

    热门问题