如何从URL解析XML文件,并将特定标签保存到文本文件中



我正在尝试通过两种方式解析XML文件,但我似乎无法工作。我认为这两种方法都非常接近,但是有些问题,我不知道什么。

from xml.dom import minidom
import urllib.request
import xml.etree.ElementTree as ET
wp = urllib.request.urlopen('https://www.boardgamegeek.com/xmlapi2/thing?id=13&stats=1') # define XML location
pw = wp.read()
#print(pw)
tree = ET.parse(pw)
root = tree.getroot()
#print(root)
with open('C:\Users\Excel\Desktop\my_text_file.txt', 'w') as f:
    f.write('IDn')
for first_heading in root.findall('items'):
    ID = first_heading.find('ratings').find('usersrated').text
    line_to_write = ID + 'n'
    with open('C:\Users\Excel\Desktop\my_text_file.txt', 'a') as f:
        f.write(line_to_write)

结果:无错误;根本什么都没有发生。

import xml.etree.ElementTree as ET
wp = urllib.request.urlopen('https://www.boardgamegeek.com/xmlapi2/thing?id=13&stats=1') # define XML location
pw = wp.read()
output_file_path = 'C:\Users\Excel\Desktop\my_text_file.txt'
f = open(output_file_path, 'wb')
print(f)
f.write('IDn')
tree = ET.parse(pw)
root = tree.getroot()
for pa in root.iter('items'):
    ArticleID = pa.find('ratings/usersrated').text
    f.write(ArticleID+'n')
f.close()

结果:typeError:需要一个字节状的对象,而不是'str'

我正在使用Python 3.6。

我让它与此一起工作...

import xml.etree.ElementTree as ET
tree = ET.parse('C:\path_to_file\Recon.xml')
root = tree.getroot()
for neighbor in root.iter('outputColumn'):
    # we are searching for this string ... '<outputColumn refId='...
    #print(neighbor.attrib) ... to confirm output
    line_to_write = str(neighbor.attrib)
    with open('C:\path_to_file\my_text_file.txt', 'a') as f:
        f.write(line_to_write)

下面的此链接非常有帮助。

https://docs.python.org/3.4/library/xml.etree.elementtree.html#xml.etree.elementtree.elementtree.xmlparser

最新更新