Skip to content

Instantly share code, notes, and snippets.

@itsuwari
Created February 16, 2017 16:57
Show Gist options
  • Save itsuwari/ced6ce924f14481427443f477970a85e to your computer and use it in GitHub Desktop.
Save itsuwari/ced6ce924f14481427443f477970a85e to your computer and use it in GitHub Desktop.
Use flask, Selenium, Chrome to get HTML or JSON, JSONP.
import os
from selenium import webdriver
from pyvirtualdisplay import Display
from flask import Flask, abord, request
app = Flask(__name__)
key = 'your secret key'
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-setuid-sandbox")
# log_prefs = { "performance":"ALL"}
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.set_script_timeout(15)
@app.route("/chrome", methods=['GET', 'POST'])
def return_html()
display = Display(visible=0, size=(900, 600))
display.start()
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.set_script_timeout(15)
url = request.args.get('url')
if not request.args.get('key') == key:
abord(403)
driver.get(url)
if request.args.get('type') in ['json', 'jsonp']:
return driver.find_element_by_tag_name('body').text
else:
return drive.page_source
driver.quit()
display.stop()
@app.errorhandler(403)
def err_403():
return 'Forbidden'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment