解析 bit.ly 的 XML 响应
我正在尝试使用 bit.ly api 进行缩短并让它工作。它向我的脚本返回一个 xml 文档。我想提取标签,但似乎无法正确解析它。
askfor = urllib2.Request(full_url)
response = urllib2.urlopen(askfor)
the_page = response.read()
所以 the_page 包含 xml 文档。我尝试过:
from xml.dom.minidom import parse
doc = parse(the_page)
这会导致错误。我做错了什么?
I was trying out the bit.ly api for shorterning and got it to work. It returns to my script an xml document. I wanted to extract out the tag but cant seem to parse it properly.
askfor = urllib2.Request(full_url)
response = urllib2.urlopen(askfor)
the_page = response.read()
So the_page contains the xml document. I tried:
from xml.dom.minidom import parse
doc = parse(the_page)
this causes an error. what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有提供错误消息,因此我无法确定这是唯一的错误。但是,
xml.minidom.parse
不接受字符串。来自parse
的文档字符串:您应该尝试:
因为
response
的行为类似于文件对象。或者您可以使用minidom
中的parseString
方法(然后将the_page
作为参数传递)。编辑:要提取 URL,您需要执行以下操作:
getElementsByTagName
的结果是所有匹配节点的列表(在本例中只有一个)。正如您所注意到的,url
是一个元素,其中包含一个子文本节点,其中包含您需要的数据。You don't provide an error message so I can't be sure this is the only error. But,
xml.minidom.parse
does not take a string. From the docstring forparse
:You should try:
since
response
will behave like a file object. Or you could use theparseString
method inminidom
instead (and then passthe_page
as the argument).EDIT: to extract the URL, you'll need to do:
The result of
getElementsByTagName
is a list of all nodes matching (just one in this case).url
is an Element as you noticed, which contains a child Text node, which contains the data you need.请参阅
xml.dom.minidom
。See the documentation for
xml.dom.minidom
.