【发布时间】: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