简体   繁体   English

Python-接下来在for循环中尝试

[英]Python - Next in for loop on try

I have a for loop that itterates over a list of URLS. 我有一个for循环,遍历URL列表。 The urls are then loaded by the chrome driver. 然后由chrome驱动程序加载网址。 Some urls will load a page that is in a 'bad' format and it will fail the first xpath test. 某些网址会加载“错误”格式的页面,并且将在第一次xpath测试中失败。 If it does I want it to go back to the next element in the loop. 如果是这样,我希望它返回到循环中的下一个元素。 My cleanup code works but I can't seem to get it to go to next element in the for loop. 我的清理代码有效,但似乎无法将其转到for循环中的下一个元素。 I have an except that closes my websbrowser but nothing I tried would all me to then loop back to 'for row in mysql_cats' 我有一个例外,它关闭了我的浏览器,但是我没有尝试过让我全部回到“ for mysql_cats中的行”

for row in mysql_cats : 
   print ('Here is the url -', row[1])
   cat_url=(row[1])
   driver = webdriver.Chrome()
   driver.get(cat_url); #Download the URL passed from mysql

   try:   
      CategoryName= driver.find_element_by_xpath('//h1[@class="categoryL3"]|//h1[@class="categoryL4"]').text   #finds either L3 or L4 catagory 
   except:
      driver.close()
      #this does close the webriver okay if it can't find the xpath, but not I cant get code here to get it to go to the next row in mysql_cats

I hope that you're closing the driver at the end of this code also if no exceptions occurs. 我希望即使没有异常发生,您也要在此代码的末尾关闭驱动程序。
If you want to start from the beginning of the loop when an exception is raised, you may add continue , as suggested in other answers: 如果要在引发异常时从循环的开头开始,则可以添加continue ,如其他答案所示:

try:   
    CategoryName=driver.find_element_by_xpath('//h1[@class="categoryL3"]|//h1[@class="categoryL4"]').text   #finds either L3 or L4 catagory 
except NoSuchElementException:
    driver.close()
    continue # jumps at the beginning of the for loop

since I do not know your code, the following tip may be useless, but a common way to handle this cases is a try/except/finally clause: 由于我不知道您的代码,因此以下技巧可能没有用,但是处理这种情况的常用方法是try/except/finally子句:

for row in mysql_cats : 
    print ('Here is the url -', row[1])
    cat_url=(row[1])
    driver = webdriver.Chrome()
    driver.get(cat_url); #Download the URL passed from mysql
    try:
        # my code, with dangerous stuff
    except NoSuchElementException:
        # handling of 'NoSuchElementException'. no need to 'continue'
    except SomeOtherUglyException:
        # handling of 'SomeOtherUglyException'
    finally: # Code that is ALWAYS executed, with or without exceptions
        driver.close()

I'm also assuming that you're creating new drivers each time for a reason. 我还假设您每次都是出于某种原因创建新的驱动程序。 If it is not voluntary, you may use something like this: 如果不是自愿的,则可以使用以下方式:

driver = webdriver.Chrome()
for row in mysql_cats : 
    print ('Here is the url -', row[1])
    cat_url=(row[1])
    driver.get(cat_url); #Download the URL passed from mysql
    try:
        # my code, with dangerous stuff
    except NoSuchElementException:
        # handling of 'NoSuchElementException'. no need to 'continue'
    except SomeOtherUglyException:
        # handling of 'SomeOtherUglyException'
driver.close()

In this way, you have only one driver that manages all the pages you're trying to open in the for loop 这样,您只有一个驱动程序可以管理您要在for循环中打开的所有页面for

have a look somewhere about how the try/except/finally is really useful when handling connections and drivers. 看看地方有关如何将try/except/finally处理的连接和驱动程序时,是非常有用的。
As a foot note, I'd like you to notice how in the code I always specify which exception I am expecting: catching all the exception can be dangerous . 作为一个脚注,我希望您注意到在代码中如何始终指定期望的异常:捕获所有异常可能很危险 BTW, probably no one will die if you simply use except: 顺便说一句,如果您只使用以下内容,可能没有人会死except:

Add continue to your except statement. 将继续添加到您的except语句。 The continue command moves you to the next cycle. Continue命令将您移至下一个循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM