Python Django-Selenium-如何在一次任务迭代后停止连接?

Python Django-Selenium-如何在一次任务迭代后停止连接?,python,django,selenium,selenium-chromedriver,Python,Django,Selenium,Selenium Chromedriver,我有一个简单的Django应用程序,集成了Selenium webscraping。 应用程序的用途-从Reddit获取图像并将URL保存在数据库中。 这项任务是按时间间隔执行的 我的问题如下: webscraping任务的第一次迭代进展顺利,但接下来所有的迭代都会引发错误。 我假设这是因为之前的连接没有中止。 我正在使用driver.quit()命令,但显然它似乎不起作用 控制台中的错误示例: urllib3.exceptions.NewConnectionError: <urllib3

我有一个简单的Django应用程序,集成了Selenium webscraping。 应用程序的用途-从Reddit获取图像并将URL保存在数据库中。 这项任务是按时间间隔执行的

我的问题如下: webscraping任务的第一次迭代进展顺利,但接下来所有的迭代都会引发错误。 我假设这是因为之前的连接没有中止。 我正在使用driver.quit()命令,但显然它似乎不起作用

控制台中的错误示例:

urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x00000289D7ADF8B0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it

urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=62138): Max retries exceeded with url: /session/6a45df20c47126554c5ef365df554676/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000022678CE5D90>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
from selenium import webdriver 
from selenium.webdriver.chrome.options import Options
from .models import Post
from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler

url2 = 'https://www.reddit.com/r/memes/new/'
chrome_driver_path = 'C:/Dev/memescraper/memescraper/static/chromedriver'
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument(" - incognito")

webdriver = webdriver.Chrome(
    executable_path=chrome_driver_path,
    options=chrome_options
    )

driver = webdriver

def fetch_reddit():
    driver.get(url2)
    meme = driver.find_elements_by_css_selector('[alt="Post image"]')
    memes = list()
    for m in meme:
        memes.append(m.get_attribute("src"))
        print(m.get_attribute("src"))

    driver.quit()

    for m in range(len(memes)):
        image = memes.pop(0)
        p = Post(image=image)
        p.save()

def start():
    scheduler = BackgroundScheduler()
    scheduler.add_job(fetch_reddit, 'interval', minutes=4)
    scheduler.start()