简体   繁体   中英

Handle multiple request from Flask with Python Selenium

I have used Flask to receive request and then run the desired actions with Selenium.

My problem is driver = webdriver.Chrome(executable_path="chromedriver") takes more time to run my request, is that a way to keep the chrome window open?

The results are as below:

在此处输入图像描述

If yes, how can I handle multiple requests from Flask if I have one Chrome window open and there are 5 requests coming in together?

Below is the loading time by running fbCreate() function with Flask from browser.

Anyone can help me on what can I do?

在此处输入图像描述

I am trying to do actions like:

  1. Create account at Facebook
  2. Get account's details from Facebook

Above actions are just a example for you to understand what I am trying to do.

from flask import Flask
from selenium import webdriver
import time

app = Flask(__name__)

@app.route('/fb/create')
def fbCreate():
    s_driver_time = time.time()

    driver = webdriver.Chrome(executable_path="chromedriver")

    start_time = time.time()

    driver.get('https://facebook.com') #example

    print("--- %.2f seconds ---" % (time.time() - start_time))
    print("--- %.2f driver seconds ---" % (time.time() - s_driver_time))

    driver.quit()

    return "done"

@app.route('/fb/account')
def fbAccount():
    driver = webdriver.Chrome(executable_path="chromedriver")

    driver.get('https://facebook.com/accounts') #example

    driver.quit()

    return "scrap data"


if __name__ == "__main__":
    app.run(host="0.0.0.0", port="8080", debug=True)

You don't need to have an individual instance of chromedriver open for every request. You can instead just start one instance of chromedriver and open multiple tabs to interweave requests as the page loads for the previous request.

You can open the tabs by using the keyboard shortcuts the browser uses.

On chrome: "ctrl"+"t" to open a tab "ctrl"+"w" to close a tab

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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