这段代码的目的是在一个列表中找到第一个元素,该列表的子元素包含三个字符串之一:“fanart”、“graphical”或“poster”。如果找到其中一个字符串,我会将包含URL文本的elements标记添加到列表中,并将movie添加到下一个字符串中。最终的结果是我得到了3个字符串,其中包含3个url到3个图像,然后我将下载这些字符串。但是,这个函数返回一个包含1项而不是3项的列表,我不知道为什么。有人能指出我做错了什么吗?为了清楚起见,我还包括了XML文件结构。在
def get_banner(target):
#Finds urls of show images
urls = []
types = ['fanart', 'graphical', 'poster']
tree = et.parse(target)
root = tree.getroot()
for banner in root.findall('Banner'):
url = 'http://thetvdb.com/banners/' + banner.find('BannerPath').text
type_ = banner.find('BannerType').text
print url
if type_ == types[0]:
urls.append(url)
break
for banner in root.findall('banner'):
url = 'http://thetvdb.com/banners/' + banner.find('BannerPath').text
type_ = banner.find('BannerType').text
print url
if type_ == types[1]:
urls.append(url)
break
for banner in root.findall('banner'):
url = 'http://thetvdb.com/banners/' + banner.find('BannerPath').text
type_ = banner.find('BannerType').text
if type_ == types[2]:
urls.append(url)
break
return urls
一。在
^{pr2}$
简单回答:
XML标记区分大小写。在
我认为您也可以通过将所有内容放在一个循环中来压缩代码:
^{pr2}$types.remove(type_)
语句应确保只向三个类型中的每一个返回第一个匹配项。在相关问题 更多 >
编程相关推荐