[英]How to insert namespace and prefixes into an XML string with Python?
[英]How do I traverse an XML tree without having to worry about namespace prefixes in Python?
例如,要读取RSS提要,此操作将不起作用,因为在'item'之前插入了愚蠢的{ http://purl.org ...}命名空间:
#!/usr/bin/env python3
import xml.etree.ElementTree as ET
import urllib, urllib.request
url = "http://some/rss/feed"
response = urllib.request.urlopen(url)
xml_text = response.read().decode('utf-8')
xml_root = ET.fromstring(xml_text)
for e in xml_root.findall('item'):
print("I found an item!")
现在由于{}前缀而使findall()变得无用,这是另一种解决方案,但这很丑陋:
#!/usr/bin/env python3
import xml.etree.ElementTree as ET
import urllib, urllib.request
url = "http://some/rss/feed"
response = urllib.request.urlopen(url)
xml_text = response.read().decode('utf-8')
xml_root = ET.fromstring(xml_text)
for e in xml_root:
if e.tag.endswith('}item'):
print("I found an item!")
我可以让ElementTree丢弃所有前缀吗?
您需要按照以下说明清楚地处理名称空间:
但是,如果相反,您将使用一个专门的库来阅读RSS feed,例如feedparser
:
>>> import feedparser
>>> url = "http://some/rss/feed"
>>> feed = feedparser.parse(url)
尽管我个人会使用XMLFeedSpider
Scrapy蜘蛛 。 作为奖励,您将获得所有其他Scrapy Web抓取框架功能 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.