6.2 XML文件
這邊用的是ElementTree.XML套件:
import xml.etree.ElementTree as ET
if __name__ == '__main__':
tree = ET.parse('example.xml')
root = tree.getroot()
print(root.attrib)
total = root.attrib['totalResults']
movies = list()
for tag in root.findall('result'):
print(tag.attrib)
movies.append(tag.attrib['title'])
print('-----')
print('There are', total, 'results in the xml file.')
print('Top 10 record:')
print('\n'.join(movies))
輸出結果:
{'totalResults': '81', 'response': 'True'}
{'title': 'Iron Man', 'year': '2008', 'imdbID': 'tt0371746', 'type': 'movie', 'poster': 'https://images-na.ssl-images-amazon.com/images/M/MV5BMTczNTI2ODUwOF5BMl5BanBnXkFtZTcwMTU0NTIzMw@@._V1_SX300.jpg'}
{'title': 'Iron Man 3', 'year': '2013', 'imdbID': 'tt1300854', 'type': 'movie', 'poster': 'https://images-na.ssl-images-amazon.com/images/M/MV5BMTkzMjEzMjY1M15BMl5BanBnXkFtZTcwNTMxOTYyOQ@@._V1_SX300.jpg'}
{'title': 'Iron Man 2', 'year': '2010', 'imdbID': 'tt1228705', 'type': 'movie', 'poster': 'https://images-na.ssl-images-amazon.com/images/M/MV5BMTM0MDgwNjMyMl5BMl5BanBnXkFtZTcwNTg3NzAzMw@@._V1_SX300.jpg'}
{'title': 'The Man in the Iron Mask', 'year': '1998', 'imdbID': 'tt0120744', 'type': 'movie', 'poster': 'https://images-na.ssl-images-amazon.com/images/M/MV5BZjM2YzcxMmQtOTc2Mi00YjdhLWFlZjUtNmFmMDQzYzU2YTk5L2ltYWdlXkEyXkFqcGdeQXVyNTAyODkwOQ@@._V1_SX300.jpg'}
{'title': 'The Man with the Iron Fists', 'year': '2012', 'imdbID': 'tt1258972', 'type': 'movie', 'poster': 'https://images-na.ssl-images-amazon.com/images/M/MV5BMTg5ODI3ODkzOV5BMl5BanBnXkFtZTcwMTQxNjUwOA@@._V1_SX300.jpg'}
{'title': 'Tetsuo, the Iron Man', 'year': '1989', 'imdbID': 'tt0096251', 'type': 'movie', 'poster': 'https://images-na.ssl-images-amazon.com/images/M/MV5BMTg2MjAzOTU3MF5BMl5BanBnXkFtZTcwOTMxODkyMQ@@._V1_SX300.jpg'}
{'title': 'The Invincible Iron Man', 'year': '2007', 'imdbID': 'tt0903135', 'type': 'movie', 'poster': 'https://images-na.ssl-images-amazon.com/images/M/MV5BOGRmZDg1YjMtMDA5YS00OTFjLTgyMjQtNDgzNTIyNzAwZDg0L2ltYWdlL2ltYWdlXkEyXkFqcGdeQXVyNTAyODkwOQ@@._V1_SX300.jpg'}
{'title': 'Iron Man: Rise of Technovore', 'year': '2013', 'imdbID': 'tt2654124', 'type': 'movie', 'poster': 'https://images-na.ssl-images-amazon.com/images/M/MV5BNDkzMTM1ODk4N15BMl5BanBnXkFtZTcwNzU0NDYxOQ@@._V1_SX300.jpg'}
{'title': 'The Man with the Iron Fists 2', 'year': '2015', 'imdbID': 'tt3625152', 'type': 'movie', 'poster': 'https://images-na.ssl-images-amazon.com/images/M/MV5BODkyMTMwMjA0Nl5BMl5BanBnXkFtZTgwMzQ3MDc4NDE@._V1_SX300.jpg'}
{'title': 'Man of Iron', 'year': '1981', 'imdbID': 'tt0082222', 'type': 'movie', 'poster': 'https://images-na.ssl-images-amazon.com/images/M/MV5BMjIyMzAzNzIwOF5BMl5BanBnXkFtZTgwMjU0MjkwMTE@._V1_SX300.jpg'}
-----
There are 81 results in the xml file.
Top 10 record:
Iron Man
Iron Man 3
Iron Man 2
The Man in the Iron Mask
The Man with the Iron Fists
Tetsuo, the Iron Man
The Invincible Iron Man
Iron Man: Rise of Technovore
The Man with the Iron Fists 2
Man of Iron
Process finished with exit code 0
原始碼點我
Last updated