簡體   English   中英

instagram 自動化的第一個腳本,如何在 for 循環中循環 else 語句?

[英]First script for automation of instagram , how to loop an else statment inside a for loop?

大家好,第一次海報長期讀者。

我的問題是我想要一個 else 語句在 for 循環中循環。

我希望 else 語句循環直到滿足上面的 if 語句?

誰能告訴我哪里出了問題?

編輯將代碼更改為while循環,而不是修剪建議,但無法逃脫while循環

for url in results:
    webdriver.get(url)
    try:
        liked = webdriver.find_elements_by_xpath("//span[@class=\"glyphsSpriteHeart__filled__24__red_5 u-__7\" and @aria-label=\"Unlike\"]")
        maxcount = 0   

        while not liked:
            sleep(1)
            webdriver.find_element_by_xpath('//span/button/span').click()
            numberoflikesgiven += 1
            maxcount += 1
            print'number of likes given : ',numberoflikesgiven
            sleep(2)
            webdriver.find_element_by_link_text('Next').click()

            if maxcount >= 10:
                print('max count reached .... moving on.')
                continue



        else:
            print ('picture has already been liked...')
            continue    

是的,你很清楚地寫了一個無限循環:

while not liked:
    ... # no assignments to "liked"

您不會在循環中的任何位置更改liked的值。 你不break循環。 因此,您有一個無限循環。 您需要在每次迭代中重新評估liked的內容。 一種典型的方法是在循環底部復制評估

        sleep(2)
        webdriver.find_element_by_link_text('Next').click()
        liked = webdriver.find_elements_by_xpath(
            "//span[@class=\"glyphsSpriteHeart__filled__24__red_5 u-__7\" \
            and @aria-label=\"Unlike\"]")

另請注意,您的continue什么也不做,因為它位於循環的底部。 只需讓您的代碼自然地到達底部,而while will continue on its own. If you intended to go to the next iteration of will continue on its own. If you intended to go to the next iteration of for url 在 results 中, then you need to打破the循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM