我试图解析一些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%;
您可以匹配具有该属性的元素并移除该部分。你知道吗
我拆分了
;
上的style属性(仅适用于相关标记),然后重新组合,排除不需要的对但是您可以很容易地更新
word-spacing
的值阅读:
我猜您可能想
re.sub
变量word-spacing
:输出
相关问题 更多 >
编程相关推荐