python 操作xml、html文件
简介
在一些项目中可能会使用到解析html文件,尤其是爬虫相关的,需要解析获取到的html内容,通常我们会使用lxml模块去进行html文件的解析。
html文件
当前存在一个简单的html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="test">
<ul class="ul-list">
<li class="li-test">li-text</li>
<li class="li-test">li-text2</li>
<li class="li-test">li-text3</li>
<li class="li-test">li-text4</li>
</ul>
<ul class="ul-list2">
<li class="li-test">li-text</li>
<li class="li-test">li-text2</li>
<li class="li-test">li-text3</li>
<li class="li-test">li-text4</li>
</ul>
</div>
</body>
</html>
获取html中的部分标签的属性及内容
from lxml import etree
import pathlib
html_path = pathlib.Path(__file__).parent.joinpath("test.html")
html = etree.parse(html_path, parser=etree.HTMLParser())
# xpath定位元素,此处为获取div class属性为test下面的所有class属性为ul-list的ul下面的class属性为li-test的所有li元素
content = html.xpath("//div[@class='test']/ul[@class='ul-list']/li[@class='li-test']")
for item in content:
# 获取标签的名称
print(item.tag)
# 获取标签中间的文本内容
print(item.text)
# 获取标签的所有属性,以字典形式返回
print(item.attrib)
li
li-text
{'class': 'li-test'}
li
li-text2
{'class': 'li-test'}
li
li-text3
{'class': 'li-test'}
li
li-text4
{'class': 'li-test'}
xml文件
存在如下xml文件
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<HTMLCodeStyleSettings>
<option name="HTML_SPACE_INSIDE_EMPTY_TAG" value="true" />
<option name="HTML_ENFORCE_QUOTES" value="true" />
</HTMLCodeStyleSettings>
<JSCodeStyleSettings version="0">
<option name="FORCE_SEMICOLON_STYLE" value="true" />
<option name="SPACE_BEFORE_FUNCTION_LEFT_PARENTH" value="false" />
<option name="FORCE_QUOTE_STYlE" value="true" />
<option name="ENFORCE_TRAILING_COMMA" value="Remove" />
<option name="SPACES_WITHIN_OBJECT_LITERAL_BRACES" value="true" />
<option name="SPACES_WITHIN_IMPORTS" value="true" />
</JSCodeStyleSettings>
<TypeScriptCodeStyleSettings version="0">
<option name="FORCE_SEMICOLON_STYLE" value="true" />
<option name="SPACE_BEFORE_FUNCTION_LEFT_PARENTH" value="false" />
<option name="FORCE_QUOTE_STYlE" value="true" />
<option name="ENFORCE_TRAILING_COMMA" value="Remove" />
<option name="SPACES_WITHIN_OBJECT_LITERAL_BRACES" value="true" />
<option name="SPACES_WITHIN_IMPORTS" value="true" />
</TypeScriptCodeStyleSettings>
<VueCodeStyleSettings>
<option name="INTERPOLATION_NEW_LINE_AFTER_START_DELIMITER" value="false" />
<option name="INTERPOLATION_NEW_LINE_BEFORE_END_DELIMITER" value="false" />
</VueCodeStyleSettings>
<codeStyleSettings language="HTML">
<option name="SOFT_MARGINS" value="100" />
<indentOptions>
<option name="CONTINUATION_INDENT_SIZE" value="4" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="JavaScript">
<option name="SOFT_MARGINS" value="100" />
</codeStyleSettings>
<codeStyleSettings language="TypeScript">
<option name="SOFT_MARGINS" value="100" />
</codeStyleSettings>
<codeStyleSettings language="Vue">
<option name="SOFT_MARGINS" value="100" />
<indentOptions>
<option name="INDENT_SIZE" value="4" />
<option name="TAB_SIZE" value="4" />
</indentOptions>
</codeStyleSettings>
</code_scheme>
</component>
获取xml中的部分标签的属性及内容
from lxml import etree
import pathlib
xml_path = pathlib.Path(__file__).parent.joinpath("test.xml")
html = etree.parse(xml_path, parser=etree.XMLParser())
content = html.xpath(
"/component[@name='ProjectCodeStyleConfiguration']/ \
code_scheme[@name]/HTMLCodeStyleSettings//option"
)
for item in content:
# 获取标签的名称
print(item.tag)
# 获取标签中间的文本内容
print(item.text)
# 获取标签的所有属性,以字典形式返回
print(item.attrib)
option
None
{'name': 'HTML_SPACE_INSIDE_EMPTY_TAG', 'value': 'true'}
option
None
{'name': 'HTML_ENFORCE_QUOTES', 'value': 'true'}
与html解析类似,主要是对于xpath语法的熟悉
补充
以上均需要xpath语法支撑,详细可见:xpath语法