Raf*_*ler 5 python xml wordpress lxml elementtree
我正在尝试使用lxml.etree来解析Wordpress导出文档(它的XML,有点像RSS).我只对已发布的帖子感兴趣,所以我使用以下内容来浏览已发布的帖子:
for item in data.findall("item"):
if item.find("wp:post_type").text != "post":
continue
if item.find("wp:status").text != "publish":
continue
write_post(item)
Run Code Online (Sandbox Code Playgroud)
找到data所有item标签的标签在哪里.item标签包含帖子,页面和草稿.我的问题是lxml找不到:名字中有标签的标签(例如wp:post_type).当我尝试时,item.find("wp:post_type")我收到此错误:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "lxml.etree.pyx", line 1279, in lxml.etree._Element.find (src/lxml/lxml.e
tree.c:38124)
File "/usr/lib64/python2.7/site-packages/lxml/_elementpath.py", line 210, in f
ind
it = iterfind(elem, path)
File "/usr/lib64/python2.7/site-packages/lxml/_elementpath.py", line 200, in i
terfind
selector = _build_path_iterator(path)
File "/usr/lib64/python2.7/site-packages/lxml/_elementpath.py", line 184, in _
build_path_iterator
selector.append(ops[token[0]](_next, token))
KeyError: ':'
Run Code Online (Sandbox Code Playgroud)
我假设KeyError : ':'标签名称中的冒号是无效的.有什么方法可以逃脱冒号,以便lxml找到正确的标签?请问:在这种情况下一些特殊的含义?或者我做错了什么?任何帮助,将不胜感激.
这:是一个XML命名空间分隔符.要在lxml中转义冒号,需要将其替换为大括号内的命名空间URL,如item.find("{http://example.org/}status").text.
| 归档时间: |
|
| 查看次数: |
3135 次 |
| 最近记录: |