Python 为我的web scraper传递循环中的错误

Python 为我的web scraper传递循环中的错误,python,selenium,Python,Selenium,我目前有一个循环运行我的网页刮板。如果它遇到错误,即无法加载页面,我将其设置为忽略它并继续循环 for i in links: try: driver.get(i); d = driver.find_elements_by_xpath('//p[@class = "list-details__item__date"]') s = driver.find_elements_by_xpath('//p[@class = "list-details__item__score"]

我目前有一个循环运行我的网页刮板。如果它遇到错误,即无法加载页面,我将其设置为忽略它并继续循环

for i in links:
try:
    driver.get(i);
    d = driver.find_elements_by_xpath('//p[@class = "list-details__item__date"]')
    s = driver.find_elements_by_xpath('//p[@class = "list-details__item__score"]')
    m = driver.find_elements_by_xpath('//span[@class="list-breadcrumb__item__in"]')
    o = driver.find_elements_by_xpath('//tr[@data-bid]');
    l = len(o)
    lm= len(m)
    for i in range(l):
        a = o[i].text
        for i in range(lm):
            b = m[i].text
            c = s[i].text
            e = d[i].text
            odds.append((a,b,c,e))
except:
    pass
然而,我现在希望在遇到错误时有一个某种注释,这样我就可以看到哪些页面没有加载。即使它们在输出表中留空,也没关系


谢谢您的帮助。

您可以为异常添加一个捕获,然后对该捕获执行一些操作。这应该适合您的脚本

import ... (This is where your initial imports are)
import io
import trackback 

for i in links:
    try:
        driver.get(i);
        d = driver.find_elements_by_xpath('//p[@class = "list-details__item__date"]')
        s = driver.find_elements_by_xpath('//p[@class = "list-details__item__score"]')
        m = driver.find_elements_by_xpath('//span[@class="list-breadcrumb__item__in"]')
        o = driver.find_elements_by_xpath('//tr[@data-bid]');
        l = len(o)
        lm= len(m)
        for i in range(l):
            a = o[i].text
            for i in range(lm):
                b = m[i].text
                c = s[i].text
                e = d[i].text
               odds.append((a,b,c,e))
    except Exception as error_script:
        print(traceback.format_exc())
        odds.append('Error count not add')
本质上,发生的是使用异常异常作为error\u script:line捕捉异常。之后,您可以使用traceback.format_exc`命令将实际错误消息打印到控制台


但最重要的是,您可以通过在异常捕获中传递append语句并在异常结束时使用pass将字符串追加到列表中。pass将在捕获时运行代码,然后转到下一个迭代

你的压痕好像有点不对劲。我不确定这将按原样运行。谢谢您的编辑。在你完成你的作业之前,我正在修复压痕。