123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- # -*-coding:utf-8-*-
- import time
- from selenium import webdriver
- # from seleniumwire import webdriver
- from selenium.webdriver.chrome.options import Options
- # 通过Keys模拟键盘
- from selenium.webdriver.common.keys import Keys
- from flask import Flask, jsonify, render_template
- from flask import request as flask_request
- g_browser_type = 1;
- app = Flask(__name__);
- @app.route('/proxy', methods=['POST'])
- def proxy():
- if not flask_request.is_json:
- return jsonify({'code': 100, 'message': '请使用JSON数据格式'}), 400;
- data = flask_request.get_json();
- if 'url' not in data:
- return jsonify({'code': 101, 'message': '缺少参数url'}), 200;
- url = data['url'];
- headers = None;
- timeout = 10;
- needs = [];
- user_agent = flask_request.headers.get('User-Agent');
- if 'needs' in data:
- needs = data['needs'];
- if 'headers' in data:
- headers = data['headers'];
- if 'timeout' in data:
- headers = data['timeout'];
- if g_browser_type == 1:
- executable_path = r'./chrome-headless-shell-win64/chrome-headless-shell.exe';
- options = Options();
- options.add_argument("--headless"); # 如果你想在无头模式下运行
- if user_agent:
- options.add_argument('user-agent=' + user_agent)
- options.binary_location = executable_path;
- # 加载策略为'eager',等待页面的所有资源完全加载完成,包括图片等
- options.page_load_strategy = 'eager'; # 或者'normal'或者'none'
- driver = webdriver.Chrome(options=options);
- else:
- executable_path = r'./phantomjs-2.1.1-windows/bin/phantomjs.exe';
- driver = webdriver.PhantomJS(executable_path=executable_path);
- # driver.implicitly_wait(timeout);
- driver.get(url);
- final_url = None;
- rsp_headers = None;
- content = None;
- try:
- # driver_wait = WebDriverWait(driver,timeout);
- # element = driver_wait.until(lambda x: x.find_element_by_xpath(Xpath));
- # 获取最终重定向后的URL
- final_url = driver.current_url;
- content = driver.page_source;
- # 获取HTTP头部信息
- rsp_headers = None;
- # for one_req in driver.requests:
- # if one_req.response:
- # print(
- # one_req.url,
- # one_req.response.status_code,
- # one_req.response.headers
- # );
- except Exception as e:
- print(e);
- finally:
- driver.close();
- driver.quit();
- result = {
- 'url': final_url,
- 'headers': None,
- 'content': None,
- };
- if 'headers' in needs:
- result['headers'] = rsp_headers;
- else:
- del result['headers'];
- if 'content' in needs:
- result['content'] = content;
- else:
- del result['content'];
- return jsonify({'code': 200, 'data': result,'message': '成功'}), 200;
- def main():
- app.config['DEBUG'] = True;
- app.run(host='0.0.0.0', port=5000);
- if __name__ == '__main__':
- main();
|