我有一个从url获取xml表的程序。这个xml上有大量的数据,因此我只能看到2500‘概要’,如果你愿意的话。
在这些xml配置文件中,我要求程序提取每个用户的ID号,这是一个8位代码。我还要求程序将url提取到接下来的2500个概要文件,这是我使用endswith()函数完成的。
我的问题是,在数据的最后一页中,没有可匹配的链接,我要求循环停止,同时也提取最后一组ID
以下是我到目前为止所拥有的:
myURL = 'blah'
while myUrl is not '':
info = request.get(myUrl)将其转换为字符串列表。
end_of_new_link = "thingy"
for link in list
if link.endswith(end_of_new_link)
myUrl = link我将链接格式化,以便在while循环的下一次迭代中使用它。
elif link.startswith(IDNUMBER)
listIDs.append(link)是否可以将变量myUrl设置为空字符串以退出while循环,还是这里的逻辑都错了?
发布于 2015-11-17 17:34:21
我认为最简单的方法是有两个变量而不是一个变量。
lastUrl, nextUrl = None, 'blah'
while nextUrl != lastUrl:
# url gets consumed and becomes "old"
info, lastUrl = request.get(nextUrl), nextUrl稍后..。
end_of_new_link = "thingy"
for link in list
if link.endswith(end_of_new_link)
nextUrl = link # now it's different so the loop will continue当然,如果您想要这样做的话,您可以进行不必要的抽象,并且有一个包装器对象,如果它的封装数据自上次读取以来已经更改(或仅仅是设置),就会标记它。
https://stackoverflow.com/questions/33763135
复制相似问题