ajax-hook+ selenium抓取带参数的 Ajax 数据
发布日期:2021-07-01 06:11:12 浏览次数:2 分类:技术文章

本文共 1370 字,大约阅读时间需要 4 分钟。

环境

依赖安装

pip install flask-cors flask selenium

安装chromedriver

实现代码

1、hook.js

监听 XMLHttpRequest 请求

// 打开链接,复制代码到这里// https://unpkg.com/ajax-hook@2.0.3/dist/ajaxhook.min.js// https://unpkg.com/axios/dist/axios.min.jsah.proxy({
//请求成功后进入 onResponse: (response, handler) => {
if (response.config.url.startsWith('/api/movie')) {
axios.post('http://localhost:5000/receiver/movie', {
url: window.location.href, data: response.response }) console.log(response.response) handler.next(response) } }})

2、main.py

驱动chrome

# -*- coding: utf-8 -*-from selenium import webdriverimport timebrowser = webdriver.Chrome()browser.get('https://dynamic2.scrape.center/')browser.execute_script(open('hook.js').read())time.sleep(2)for index in range(3):    print('current page', index)    btn_next = browser.find_element_by_css_selector('.btn-next')    btn_next.click()    time.sleep(2)browser.close()browser.quit()

3、server.py

接收数据的服务,可以进一步将数据存入数据库

# -*- coding: utf-8 -*-import jsonfrom flask import Flask, request, jsonifyfrom flask_cors import CORSapp = Flask(__name__)CORS(app)@app.route('/receiver/movie', methods=['POST'])def receive():    content = json.loads(request.data)    print(content)    # to something    return jsonify({
'status': True})if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=True)

参考

转载地址:https://pengshiyu.blog.csdn.net/article/details/106613801 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Idea+maven-jar-plugin构建JavaFX可执行jar包
下一篇:js文件中引入另一个js文件

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年05月01日 13时15分20秒