我正在编写一个脚本来从我的 Flickr 帐户中提取其中所有照片的选定元数据。当脚本在照片集中移动时,我得到一个(看似)随机的ElementTree.ParseError
:
File "flickr-get.py", line 95, in <module>
get_photoset_metadata(key)
File "flickr-get.py", line 51, in get_photoset_metadata
photo = flickr.photos_getInfo(photo_id=node.get('id'), api_key=mdah_api_key)
File "/usr/lib/pymodules/python2.7/flickrapi/__init__.py", line 337, in handler
parse_format=args['format'], **args)
File "/usr/lib/pymodules/python2.7/flickrapi/__init__.py", line 423, in __wrap_in_parser
return parser(self, data)
File "/usr/lib/pymodules/python2.7/flickrapi/__init__.py", line 258, in parse_etree
rsp = ElementTree.fromstring(rest_xml)
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1301, in XML
return parser.close()
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1654, in close
self._raiseerror(v)
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1506, in _raiseerror
raise err
xml.etree.ElementTree.ParseError: no element found: line 1, column 0
我说随机是因为这个异常会发生在照片处理中不可预测的点。
似乎它是 XML 中的缺失值,或者是可以预测的编码问题,所以我不知道如何解决它。我知道我远远低于每小时通话次数的限制。
# -*- coding: utf-8 -*-
import argparse
import flickrapi
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
import csv
mdah_api_key = 'something'
mdah_api_secret = 'something'
fileencoding = "iso-8859-1"
flickr = flickrapi.FlickrAPI(mdah_api_key, mdah_api_secret)
# authorize user
(token, frob) = flickr.get_token_part_one(perms='write')
if not token:
raw_input('Press ENTER after you authorized this program')
flickr.get_token_part_two((token, frob))
photosets_dict = dict()
f = open('flickr-data.csv', 'w')
writer = csv.writer(f)
parser = argparse.ArgumentParser(description='export metadata for Flickr user by set (or all photos if no set specified)')
parser.add_argument('-u','--user', default='something', help='target Flickr user')
parser.add_argument('-p','--photoset', default='all', help='target photoset')
args = parser.parse_args()
def get_photoset_metadata(photoset):
photoset_info_tree = ET.ElementTree(flickr.photosets_getInfo(api_key=mdah_api_key, photoset_id=photoset))
photoset_tree = ET.ElementTree(flickr.photosets_getPhotos(api_key=mdah_api_key, photoset_id=photoset))
photo_ids = []
data = []
data_row = []
for title in photoset_info_tree.iter('title'):
set_title = title.text.decode(fileencoding)
# grab all photo ids in set
for node in photoset_tree.iter('photo'):
# get each photo
photo = flickr.photos_getInfo(photo_id=node.get('id'), api_key=mdah_api_key)
photo_tree = ET.ElementTree(photo)
data_row.append(set_title)
for title in photo_tree.iter('title'):
data_row.append(title.text.decode(fileencoding))
try:
geodata = flickr.photos_geo_getLocation(photo_id=node.get('id'), api_key=mdah_api_key)
geodata_tree = ET.ElementTree(geodata)
for geonode in geodata_tree.iter('location'):
data_row.append(geonode.get('latitude').decode(fileencoding))
data_row.append(geonode.get('longitude').decode(fileencoding))
data_row.append(geonode.get('accuracy').decode(fileencoding))
for geonode in geodata_tree.iter('country'):
data_row.append(geonode.text.decode(fileencoding))
for geonode in geodata_tree.iter('region'):
data_row.append(geonode.text.decode(fileencoding))
for geonode in geodata_tree.iter('county'):
data_row.append(geonode.text.decode(fileencoding))
for geonode in geodata_tree.iter('locality'):
data_row.append(geonode.text.decode(fileencoding))
for geonode in geodata_tree.iter('neighbourhood'):
data_row.append(geonode.text.decode(fileencoding))
except Exception as e:
e = True
writer.writerow(data_row)
print data_row
data_row = []
if args.photoset == 'all':
photosets_tree = ET.ElementTree(flickr.photosets_getList(api_key=mdah_api_key, user_id=args.user))
# grab all photosets
for node in photosets_tree.iter('photoset'):
photosets_dict[node.get('id')] = node[0].text
# iterate over photosets dictionary
for key, value in photosets_dict.items():
print '-- Exporting metadata from ' + key + ', ' + value + ' --'
get_photoset_metadata(key)
else:
get_photoset_metadata(args.photoset)
这是flickr.photos.getInfo
XML 返回的前几行:
<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<photo id="8009597985" secret="something" server="8182" farm="9" dateuploaded="1348248234" isfavorite="0" license="7" safety_level="0" rotation="0" originalsecret="something" originalformat="png" views="97" media="photo">
<owner nsid="77015680@N05" username="Mississippi Department of Archives and History" realname="" location="" iconserver="5324" iconfarm="6" path_alias="mississippi-dept-of-archives-and-history" />
<title>Memphis Warehouse Company.</title>