I'm trying to create a webscraping Flask app that creates a new Webdriver instance for each user session, so that different users can scrape content from different pages. This would be simpler if the driver.get()
and data collection happened in the same API call, but they can't due to the nature of the scraping I'll be doing. Here's what I have so far:
from flask import Flask, session
from flask_session import Session
app = Flask(__name__)
SESSION_TYPE = 'filesystem'
app.config.from_object(__name__)
Session(app)
@app.route('/get_site/<link>', methods=['GET'])
def get_site(link):
session['driver'] = webdriver.Chrome(options=options)
session['driver'].get(link)
return 'link opened!' # confirmation message
@app.route('/scrape_current_site', methods=['GET'])
def scrape_current_site():
return session['driver'].title # collecting arbitrary data from page
app.run()
This doesn't work, though:
AttributeError: Can't pickle local object '_createenviron.<locals>.encode'
Conceptually a flask session feels like it's what I'm looking for (a new, unique object for each session), but I can't figure out how to get it to work.