我试图解析这个:http ://www.codespot.blogspot.in/atom.xml?redirect=false&start-index=1&max-results=500
问题是:
我必须将 xml 存储在一个文件中,以便 ElementTree 解析它。如何避免它并仅解析来自 GET 请求的字符串响应?
尽管我正在这样做,但要获得所有标题,它仍然不起作用:
f = open('output.xml','wb+') f.write(r.content) f.close() tree = "" with open('output.xml', 'rt') as f: tree = ElementTree.parse(f) print tree root = tree.getroot() for elem in tree.iter(): print elem.tag, elem.attrib for atype in tree.findall('title'): print atype.contents