python - 如何在 Python 中进行 xml 解析?
问题描述
我有一个“.odf”文件。
我只想分离href的“Text/Chapter1.xhtml”。
我该怎么做?
这是样本。
我有一个“.odf”文件。
我只想分离href的“Text/Chapter1.xhtml”。
我该怎么做?
这是样本。
<?xml version="1.0" encoding="utf-8"?>
<package version="2.0" unique-identifier="BookId" xmlns="http:/pf">
<metadata xmlns:dc="http:ts/1.1/" xmlns:opf="ht200pf">
<dc:identifier opf:scheme="ISBN" id="BookId">urn:19be</dc:identifier>
<dc:title>samplesample</dc:title>
<dc:creator />
<dc:language>ko</dc:language>
<meta name="cover" content="image" />
<meta content="0.9.18" name="Sigil version" />
<dc:date opf:event="modification" xmlns:opf="httopf">2019-12-12</dc:date>
</metadata>
<manifest>
<item id="tocncx" href="toc.ncx" media-type="application/xhtml+xml"/>
<item id="titlepage" href="Text/titlepage.xhtml" media-type="application/xhtml+xml"/>
<item id="chapter1" href="Text/chapter1.xhtml" media-type="application/xhtml+xml"/>
<item id="chapter2" href="Text/chapter2.xhtml" media-type="application/xhtml+xml"/>
<item id="chapter3" href="Text/chapter3.xhtml" media-type="application/xhtml+xml"/>
<item id="chapter4" href="Text/chapter4.xhtml" media-type="application/xhtml+xml"/>
<item id="chapter5" href="Text/chapter5.xhtml" media-type="application/xhtml+xml"/>
<item id="chapter6" href="Text/chapter6.xhtml" media-type="application/xhtml+xml"/>
</manifest>
<spine toc="tocncx">
<itemref idref="titlepage"/>
<itemref idref="chapter1"/>
<itemref idref="chapter2"/>
<itemref idref="chapter3"/>
<itemref idref="chapter4"/>
<itemref idref="chapter5"/>
<itemref idref="chapter6"/>
</spine>
</package>
解决方案
不知道有没有你想要的。
from simplified_scrapy import SimplifiedDoc,req,utils
html='''
<?xml version="1.0" encoding="utf-8"?>
<package version="2.0" unique-identifier="BookId" xmlns="http:/pf">
<metadata xmlns:dc="http:ts/1.1/" xmlns:opf="ht200pf">
<dc:identifier opf:scheme="ISBN" id="BookId">urn:19be</dc:identifier>
<dc:title>samplesample</dc:title>
<dc:creator />
<dc:language>ko</dc:language>
<meta name="cover" content="image" />
<meta content="0.9.18" name="Sigil version" />
<dc:date opf:event="modification" xmlns:opf="httopf">2019-12-12</dc:date>
</metadata>
<manifest>
<item id="tocncx" href="toc.ncx" media-type="application/xhtml+xml"/>
<item id="titlepage" href="Text/titlepage.xhtml" media-type="application/xhtml+xml"/>
<item id="chapter1" href="Text/chapter1.xhtml" media-type="application/xhtml+xml"/>
<item id="chapter2" href="Text/chapter2.xhtml" media-type="application/xhtml+xml"/>
<item id="chapter3" href="Text/chapter3.xhtml" media-type="application/xhtml+xml"/>
<item id="chapter4" href="Text/chapter4.xhtml" media-type="application/xhtml+xml"/>
<item id="chapter5" href="Text/chapter5.xhtml" media-type="application/xhtml+xml"/>
<item id="chapter6" href="Text/chapter6.xhtml" media-type="application/xhtml+xml"/>
</manifest>
<spine toc="tocncx">
<itemref idref="titlepage"/>
<itemref idref="chapter1"/>
<itemref idref="chapter2"/>
<itemref idref="chapter3"/>
<itemref idref="chapter4"/>
<itemref idref="chapter5"/>
<itemref idref="chapter6"/>
</spine>
</package>'''
doc = SimplifiedDoc(html)
hrefs = doc.manifest.selects('item').select('href()')
print (hrefs)
href = doc.manifest.select("item#chapter1>href()")
print (href)
item = doc.manifest.select("item#chapter1")
print (item)
结果:
['toc.ncx', 'Text/titlepage.xhtml', 'Text/chapter1.xhtml', 'Text/chapter2.xhtml', 'Text/chapter3.xhtml', 'Text/chapter4.xhtml', 'Text/chapter5.xhtml', 'Text/chapter6.xhtml']
Text/chapter1.xhtml
{'id': 'chapter1', 'href': 'Text/chapter1.xhtml', 'media-type': 'application/xhtml+xml', 'tag': 'item'}
推荐阅读
- docker - 使用 Jenkins-Pipelines 在现有 Docker 中运行命令
- windows - 更改机器名称后 IIS 失败
- facebook - Facebook SDK - 具有试用期和折扣期的订阅
- google-maps - 修复中心的谷歌地图标记
- php - Symfony 4 原则 - 为新数据库表创建实体和控制器
- c++ - ESP32 到 ESP32 AP/客户端 WiFi 连接问题
- html - HTML中的字段集和图例标签?
- laravel - 作曲家调试
- git - Git合并:将冲突作为单独的文件获取以与外部工具合并
- laravel-5 - 选择具有 2 个空选项值的输入