Python 如何将提要聚合到一个字符串中,然后解析它们?

Python 如何将提要聚合到一个字符串中,然后解析它们?,python,xml,concatenation,feed,Python,Xml,Concatenation,Feed,我试图聚合几个youtube提要,连接它们,然后解析提要。 当我自己解析单个feed时,我没有遇到任何问题,代码似乎可以工作。但是,当我尝试将提要聚合为一个长字符串,然后使用etree.fromstring(aggregate\u partner\u提要)时,我得到了一个错误。我得到的错误是ParseError:unbound前缀和etree行(前面引用的)作为错误给出。有关于如何解决这个问题的建议吗 aggregated_partners_list = [cnn, teamcoco, buzz

我试图聚合几个youtube提要,连接它们,然后解析提要。 当我自己解析单个feed时,我没有遇到任何问题,代码似乎可以工作。但是,当我尝试将提要聚合为一个长字符串,然后使用etree.fromstring(aggregate\u partner\u提要)时,我得到了一个错误。我得到的错误是ParseError:unbound前缀和etree行(前面引用的)作为错误给出。有关于如何解决这个问题的建议吗

aggregated_partners_list = [cnn, teamcoco, buzzfeed]


i = 1 
number_of_partners = len(aggregated_partners_list)
aggregate_partner_feed = '' 

for entry in aggregated_partners_list:
    #YOUTUBE FEED
    #download the file:
    file = urllib2.urlopen('http://gdata.youtube.com/feeds/api/users/'+entry+'/uploads?v=2&max-results=50')
    #convert to string:
    data = file.read()
    #close file because we dont need it anymore:
    file.close()

    if i == 1:
        #remove ending </feed>
        data = data[:-7]

    if i>1 and i != number_of_partners:
        data = data[data.find('<entry'):]
        data = data[:-7]
        #remove everything before first <entry> in the new feed and the last </entry>

    #if last, then only remove everything before first <entry>
    if i == number_of_partners:
        data = data[data.find('<entry'):]

    #append the current feed to the existing feed
    aggregate_partner_feed += data

    #increment the counter  
    i=i+1

print isinstance(data, basestring)                      #returns true
print isinstance(aggregate_partner_feed, basestring)    #returns true

#apply the parsing to the aggregated feed

#entire feed
root = etree.fromstring(aggregate_partner_feed)     #this is the line that give an error
#all entries
entries = root.findall('{http://www.w3.org/2005/Atom}entry')
#more code that seems to work...
aggregated\u partners\u list=[cnn、teamcoco、buzzfeed]
i=1
合作伙伴数量=len(聚合合作伙伴列表)
聚合\u伙伴\u提要=“”
对于聚合合作伙伴列表中的条目:
#YOUTUBE提要
#下载文件:
file=urlib2.urlopen('http://gdata.youtube.com/feeds/api/users/“+entry+”/上传?v=2,最大结果=50”)
#转换为字符串:
data=file.read()
#关闭文件,因为我们不再需要它:
file.close()文件
如果i==1:
#删除结尾
数据=数据[:-7]
如果i>1,则i!=合作伙伴的数量:

data=data[data.find(“我分别解析了每个提要,然后使用。追加而不是将字符串连接在一起然后进行解析。

能否显示
aggregate\u partner\u feed
的值?您可以使用etree单独解析每个提要,并将解析后的条目追加到组合树对象中,而不是手动将xml作为string@J.F.Sebastian如何将已解析的条目附加到组合树对象?使用而不是解析原始提要可能有助于简化代码。@sharataka:每个元素都是其子元素的集合,例如,有
.append()
方法