在条件相同的循环完成第一次迭代后,尝试从头开始重新开始循环。

时间:2019-06-16 15:16:04

标签: python-3.x loops for-loop

我有2个嵌套的“ for循环”,我尝试重新启动第一个循环以从头开始进行迭代,一旦其嵌套循环完成,则按照if语句成功进行迭代。 但是,如果嵌套循环满足“其他”条件,则外循环应定期继续其迭代。 我试图告诉代码,如果找到的keyword_on_screen从创建的关键字开始直到该点继续,否则要在它们停止匹配的最后一个索引处更改后者。

这是我已经尝试过的方法,但是第一个(外)循环会继续迭代字母,而不是从头开始。

import time
from bs4 import BeautifulSoup
from selenium import webdriver
browser = webdriver.Chrome('chromedriver.exe')
keyList = ['a','b','c','d']
mainUrl = "https://www.amazon.com/"
browser.get(mainUrl)
searchInput = browser.find_element_by_xpath('//input[@id="twotabsearchtextbox"]')
searchInput.clear()
keyword = str()

# I WANT THE LOOP TO RESTART FROM HERE ONCE THE NESTED LOOP IS FINISHED

for keyChar in keyList:
    searchInput.send_keys(keyChar)
    time.sleep(2)
    searchSoup = BeautifulSoup(browser.page_source, "html.parser")
    searchResult = searchSoup.find_all('div', attrs={'id':'suggestions-template'})
    results = searchResult[0].find_all('div', attrs={'class':'s-suggestion'})
    keyword = keyword + keyChar
    print("This is the keyword so far:>>>>   ",keyword)
    time.sleep(2)

    for data_result in results:
        data_keywords = BeautifulSoup(str(data_result),"html.parser")
        keywords_on_screen = data_keywords.find("div", class_="s-suggestion")["data-keyword"]
        print(keywords_on_screen)

        if keywords_on_screen.startswith(keyword):
            print('ALL GOOD')
        else:
            print('Condition not met, moving on next letter')
            continue

1 个答案:

答案 0 :(得分:1)

我相信,如果满足嵌套循环中的else条件,您要继续最外层的循环。如果不符合else条件,请从头开始重新启动最外面的循环。

尝试:-

import time
from bs4 import BeautifulSoup
from selenium import webdriver
browser = webdriver.Chrome('chromedriver.exe')
keyList = ['a','b','c','d']
mainUrl = "https://www.amazon.com/"
browser.get(mainUrl)
searchInput = browser.find_element_by_xpath('//input[@id="twotabsearchtextbox"]')
searchInput.clear()
keyword = str()
ctr = 0
x = 0

# I WANT THE LOOP TO RESTART FROM HERE ONCE THE NESTED LOOP IS FINISHED

while x < len(keyList):
    keyChar = keyList[x]
    searchInput.send_keys(keyChar)
    time.sleep(2)
    searchSoup = BeautifulSoup(browser.page_source, "html.parser")
    searchResult = searchSoup.find_all('div', attrs={'id':'suggestions-template'})
    results = searchResult[0].find_all('div', attrs={'class':'s-suggestion'})
    keyword = keyword + keyChar
    print("This is the keyword so far:>>>>   ",keyword)
    time.sleep(2)

    for data_result in results:
        data_keywords = BeautifulSoup(str(data_result),"html.parser")
        keywords_on_screen = data_keywords.find("div", class_="s-suggestion")["data-keyword"]
        print(keywords_on_screen)

        if keywords_on_screen.startswith(keyword):
            print('ALL GOOD')
            ctr = 1
        else:
            print('Condition not met, moving on next letter')
            ctr = 0
            continue
    if ctr is not 1:
        x += 1

最外面的循环将无限执行,直到满足嵌套if的条件为止;否则,如果不满足该条件,则最外面的循环将正常执行,并且一旦列表KeyList的所有元素都将终止筋疲力尽。