python lxml:选择性删除内联样式属性的语法?
python lxml: syntax for selectively deleting inline style attributes?
我正在使用 python 3.4 和 lxml.html 库。
我正在尝试从我使用 css 选择器定位的 html 元素中删除 border-bottom
内联样式。
这是一个显示示例 td 元素和我的选择器的代码片段:
html_snippet = lxml.html.fromstring("""<td valign="bottom" colspan="10" align="center" style="background-color:azure; border-bottom:1px solid #000000"><font style="font-family:Times New Roman" size="2">Estimated Future Payouts</font> \n <br/><font style="font-family:Times New Roman" size="2">Under Non-Equity Incentive</font> \n <br/><font style="font-family:Times New Roman" size="2">Plan Awards</font> \n </td>""")
selection = html_snippet.cssselect('td[style*="border-bottom"]')
selection.attrib['style']
>>>>'background-color: azure;border-bottom:1px solid #000000'
访问内联样式属性的正确方法是什么,以便我可以从我使用选择器定位的任何元素中删除 border-bottom
属性?
您可以将 style
属性值拆分为 ;
,创建一个 CSS 属性 名称 -> 值映射,删除 border-bottom
并通过将地图的元素与 ;
连接起来再次重建 style
属性。实施示例:
style = selection.attrib['style']
properties = dict([item.split(":") for item in style.split("; ")])
del properties['border-bottom']
selection.attrib['style'] = "; ".join([key + ":" + value for key, value in properties.items()])
print(lxml.html.tostring(selection))
我很确定您可以轻松破解此解决方案。
或者,这里有一个相当 "crazy" 的选项 - 将数据转储到 "html" 文件中,通过 selenium
在浏览器中打开文件,通过 [=33] 删除属性=] 并打印出元素的 HTML 表示:
import os
from selenium import webdriver
data = """
<td valign="bottom" colspan="10" align="center" style="background-color:azure; border-bottom:1px solid #000000"><font style="font-family:Times New Roman" size="2">Estimated Future Payouts</font> \n <br/><font style="font-family:Times New Roman" size="2">Under Non-Equity Incentive</font> \n <br/><font style="font-family:Times New Roman" size="2">Plan Awards</font> \n </td>
"""
with open("index.html", "w") as f:
f.write("<body><table><tr>%s</tr></table></body>" % data)
driver = webdriver.Chrome()
driver.get("file://" + os.path.abspath("index.html"))
td = driver.find_element_by_tag_name("td")
driver.execute_script("arguments[0].style['border-bottom'] = '';", td)
print(td.get_attribute("outerHTML"))
driver.close()
打印:
<td valign="bottom" colspan="10" align="center" style="background-color: rgb(240, 255, 255);"><font
style="font-family:Times New Roman" size="2">Estimated Future Payouts</font>
<br><font style="font-family:Times New Roman" size="2">Under Non-Equity Incentive</font>
<br><font style="font-family:Times New Roman" size="2">Plan Awards</font>
</td>
虽然在这种情况下有点矫枉过正,但有一个软件包。
import cssutils
sheet = cssutils.parseStyle('background-color: azure;border-bottom:1px solid #000000')
sheet.removeProperty('border-bottom') # returns '1px solid #000'
print(sheet.cssText)
输出background-color: azure
我正在使用 python 3.4 和 lxml.html 库。
我正在尝试从我使用 css 选择器定位的 html 元素中删除 border-bottom
内联样式。
这是一个显示示例 td 元素和我的选择器的代码片段:
html_snippet = lxml.html.fromstring("""<td valign="bottom" colspan="10" align="center" style="background-color:azure; border-bottom:1px solid #000000"><font style="font-family:Times New Roman" size="2">Estimated Future Payouts</font> \n <br/><font style="font-family:Times New Roman" size="2">Under Non-Equity Incentive</font> \n <br/><font style="font-family:Times New Roman" size="2">Plan Awards</font> \n </td>""")
selection = html_snippet.cssselect('td[style*="border-bottom"]')
selection.attrib['style']
>>>>'background-color: azure;border-bottom:1px solid #000000'
访问内联样式属性的正确方法是什么,以便我可以从我使用选择器定位的任何元素中删除 border-bottom
属性?
您可以将 style
属性值拆分为 ;
,创建一个 CSS 属性 名称 -> 值映射,删除 border-bottom
并通过将地图的元素与 ;
连接起来再次重建 style
属性。实施示例:
style = selection.attrib['style']
properties = dict([item.split(":") for item in style.split("; ")])
del properties['border-bottom']
selection.attrib['style'] = "; ".join([key + ":" + value for key, value in properties.items()])
print(lxml.html.tostring(selection))
我很确定您可以轻松破解此解决方案。
或者,这里有一个相当 "crazy" 的选项 - 将数据转储到 "html" 文件中,通过 selenium
在浏览器中打开文件,通过 [=33] 删除属性=] 并打印出元素的 HTML 表示:
import os
from selenium import webdriver
data = """
<td valign="bottom" colspan="10" align="center" style="background-color:azure; border-bottom:1px solid #000000"><font style="font-family:Times New Roman" size="2">Estimated Future Payouts</font> \n <br/><font style="font-family:Times New Roman" size="2">Under Non-Equity Incentive</font> \n <br/><font style="font-family:Times New Roman" size="2">Plan Awards</font> \n </td>
"""
with open("index.html", "w") as f:
f.write("<body><table><tr>%s</tr></table></body>" % data)
driver = webdriver.Chrome()
driver.get("file://" + os.path.abspath("index.html"))
td = driver.find_element_by_tag_name("td")
driver.execute_script("arguments[0].style['border-bottom'] = '';", td)
print(td.get_attribute("outerHTML"))
driver.close()
打印:
<td valign="bottom" colspan="10" align="center" style="background-color: rgb(240, 255, 255);"><font
style="font-family:Times New Roman" size="2">Estimated Future Payouts</font>
<br><font style="font-family:Times New Roman" size="2">Under Non-Equity Incentive</font>
<br><font style="font-family:Times New Roman" size="2">Plan Awards</font>
</td>
虽然在这种情况下有点矫枉过正,但有一个软件包。
import cssutils
sheet = cssutils.parseStyle('background-color: azure;border-bottom:1px solid #000000')
sheet.removeProperty('border-bottom') # returns '1px solid #000'
print(sheet.cssText)
输出background-color: azure