Created
September 26, 2017 03:36
-
-
Save imfht/eed9cf97f9c02196452ab884a47aef0b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask import request | |
app = Flask(__name__) | |
from flask import send_file | |
from selenium import webdriver | |
import hashlib | |
from selenium.webdriver.common.keys import Keys | |
import datetime | |
def get_driver(): | |
options = webdriver.ChromeOptions() | |
options.binary_location = '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary' | |
options.add_argument('--headless') | |
options.add_argument('--window-size=1920x1440') | |
options.add_argument('--disable-gpu') | |
driver = webdriver.Chrome(chrome_options=options, executable_path="/Users/fiht/Downloads/chromedriver") | |
return driver | |
driver = get_driver() | |
from flask import jsonify | |
def shoot_and_get_img_path(driver, url): | |
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') | |
driver.get(url) | |
time_start = datetime.datetime.now() | |
file_name = '/tmp/pngs/%s.png' % hashlib.md5(url.encode('utf-8')).hexdigest() | |
r = {} | |
if driver.save_screenshot(file_name): | |
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w') | |
return send_file(file_name, mimetype='image/gif') | |
# return {'status': 1, 'position': file_name, 'time': str(datetime.datetime.now() - time_start)} | |
else: | |
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w') | |
return jsonify({'status': 0, 'time': str(datetime.datetime.now() - time_start)}) | |
@app.route('/') | |
def hello_world(): | |
if request.args.get('url'): | |
return shoot_and_get_img_path(driver, url=request.args.get('url')) | |
return 'Hello World!' | |
if __name__ == '__main__': | |
app.run(debug=True) | |
# shoot_and_get_img_path(driver=get_driver(),url='http://www.sdu.edu.cn') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment