Python 如果语句不正确则在下一行开始 for 循环

Python start for loop on next line if a statement is not true

with open('./links.txt', 'r') as f:
    for line in f:
        browser.get(line)
        WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".jw-media")))
        title = None
        network = None
        subnetwork = None
        html = browser.page_source
        if isinstance(title, str):
            title = title.text
        else:
            with open('./notfound.txt', 'a') as h:
                h.write(line)
                h.write('\n')
                h.close()
                next(line)

对于 f 中的每一行,它将变量 title、network 和 sub network 设置为 None,并且每次页面加载 url(即每一行)来自 links.txt 它将变量设置为正确的字符串。 if 语句将检查变量是否已更改,如果没有,我希望它转到下一个字符串并从顶部开始将变量设置为 None、加载页面等,有什么办法吗这样做?

您正在寻找 https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops 中的继续,这将继续循环的下一次迭代。

您的代码的问题是 next(line) 语句没有意义,因为 line 不是迭代器(循环构造已经为您处理了递增)。此外,在 with 块内使用 h.close() 是错误的,因为当您离开 with 块时,将自动调用 close 。 最后,在循环的每次迭代中打开和关闭文件不是一个好习惯。

下面的代码解决了这些问题:

with open('./links.txt', 'r') as f:
    with open('./notfound.txt', 'a') as h:
        for line in f:
            browser.get(line.strip())   # Remove extraneous spaces, including the final '\n'
            WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".jw-media")))
            title = browser.title  # This is how you get the title of the page
            network = None
            subnetwork = None
            html = browser.page_source
            if isinstance(title, str):  # Not sure what is your goal with this line
                title = title.text
            else:
                h.write(line)  # You don't need to add the '\n' because line already has it