2015-11-17 27 views
0

我有一個程序從url中獲取一個xml表單。這個XML有很多數據,因此如果你一次只能看到2500個'profiles',退出while循環,當某個字符串不出現在Python中

在這些xml配置文件中,我需要程序提取每個用戶的8位數字代碼。我還需要該程序將URL導出到下一次使用endswith()函數完成的2500個配置文件。

我的問題是數據的最後一頁沒有聯繫,以配合我要求循環停止,同時也拉動了最後一組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 

I格式的鏈接,這樣我可以在while循環的下一次迭代使用字符串列表

 elif link.startswith(IDNUMBER) 
      listIDs.append(link) 

有沒有辦法可以設置變量myUrl空字符串退出while循環或者是我的邏輯都錯在這裏

+0

您應該解析XML,以便您可以正確閱讀它:http://stackoverflow.com/questions/1912434/how-do-i-parse-xml-in-python – kponz

+0

如果沒有鏈接不會'其他:myUrl =「」'工作? – SirParselot

回答

1

我認爲最簡單的方法就是有兩個變量,而不是一個。

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 

當然,你可以把這個抽象不必要的,如果你想和有如果封裝的數據改變了這一切標誌着一個包裝對象(或者乾脆一直設置)自上次讀取。

+1

輝煌,工作正常 – johnfk3