网店推广方法有哪些深圳企业股权优化
- 作者: 五速梦信息网
- 时间: 2026年04月20日 08:18
当前位置: 首页 > news >正文
网店推广方法有哪些,深圳企业股权优化,wordpress 页面调用标签,在网站建设论文的基本分析Selenium和Requests搭配使用 前要1. CDP2. 通过requests控制浏览器2. 1 代码一2. 2 代码2 3. 通过selenium获取cookie, requests携带cookie请求 前要 之前有提过, 用selenium控制本地浏览器, 提高拟人化,但是效率比较低,今天说一种selenium和requests搭配使用的方法 注意: 一定… Selenium和Requests搭配使用 前要1. CDP2. 通过requests控制浏览器2. 1 代码一2. 2 代码2 3. 通过selenium获取cookie, requests携带cookie请求 前要 之前有提过, 用selenium控制本地浏览器, 提高拟人化,但是效率比较低,今天说一种selenium和requests搭配使用的方法 注意: 一定要先了解怎么远程控制浏览器,之后再按照这个来 selenium控制本地浏览器(二选一) https://blog.csdn.net/weixin_44388373/article/details/121989842 https://blog.csdn.net/weixin_45081575/article/details/112621581 1. CDP CDP 全称为 Chrome Devtools-Protocol 通过执行 CDP 命令可以在网页加载前运行一段代码进而改变浏览器的指纹特征 允许使用工具来检测、检查、调试和分析 Chromium、Chrome 和其他基于 Blink 的浏览器。
通过requests控制浏览器 看这里!!! 这里值得注意是安装 websocket 模块要按照这以下顺序 pip install webscoketpip install websocket-client # 之前的代码启动浏览器,selenium调用没问题
调用方式1
C:\Program Files\Google\Chrome\Application\chrome.exe –remote-debugging-port9222 –user-data-dir随便找个空文件夹路径# 调用方式2 start chrome –remote-debugging-port9222 –user-data-dirC:\Users\1\Desktop\chrome# 代码调用 import os os.popen(start chrome –remote-debugging-port9222 –user-data-dirC:\Users\1\Desktop\chrome)# 但是如果让requests调用会出错(无权限,禁止调用)
需要在语句中加入 –remote-allow-origins*
import os os.popen(start chrome –remote-debugging-port9222 –remote-allow-origins* –user-data-dirC:\Users\1\Desktop\chrome)2. 1 代码一 这里用的是 小菜欸 大佬的文章: 【Selenium】Python Selenium 执行 CDP 我只是摘抄了一部分, 大佬讲的更详细, 推荐各位去看看
这里插入代码片
-- coding: utf-8 --
Time : 2022-08-27 12:00
Name : py_cdp.pyimport json
import requests import websocketdef websocket_conn():# websocket_conn 连接浏览器resp requests.get(http://127.0.0.1:9222/json) # 有不懂的看上一篇文章assert resp.status_code 200ws_url resp.json()[0].get(webSocketDebuggerUrl)return websocket.create_connection(ws_url)def execute_cdp(conn: websocket, command: dict):# 执行 dpconn.send(json.dumps(command))# 接受websocket的响应并将字符串转换为 dict()return json.loads(conn.recv())def main():conn websocket_conn()# js alert(hello world) # 弹窗 hello world# js console.log(hello world) # 控制台打印 hello worldjs location.hrefhttps://www.bilibili.com # 页面跳转command {method: Runtime.evaluate, # 处理 传进去的 expressionid: int(1), # id需要传一个整型否则会报错,可以随便填一个数字params: {expression: js} # 要执行的js语句}resp execute_cdp(conn, command)print(resp)if name main:main()运行效果看下面动图js代码中指定页面跳转到 B站。
2 代码2 这里用的是 合天网安实验室 的文章: 利用远程调试获取Chromium内核浏览器Cookie 这个也只摘抄了一部分, 推荐各位去看看 代码可以获取本地浏览器所有Cookie, 包括保存到本地的Cookie信息 import json import requests import websocket
添加以后发送如下数据包就可以成功获取Cookie
GET_ALL_COOKIES_REQUEST json.dumps({id: 1, method: Storage.getCookies})def hit_that_secret_json_path_like_its_1997():response requests.get(http://127.0.0.1:9222/json)websocket_url response.json()[0].get(webSocketDebuggerUrl)return websocket_urldef gimme_those_cookies(ws_url):ws websocket.create_connection(ws_url)ws.send(GET_ALL_COOKIES_REQUEST)result ws.recv()ws.close()response json.loads(result)print(response)cookies response[result][cookies]return cookiesdef to_cookie_dict(data):# namecookie的名称 必须有# valuecookie对应的值动态生成的, 必须有# domain服务器域名# expiryCookie有效终止日期# pathPath属性定义了Web服务器上哪些路径下的页面可获取服务器设置的Cookie# httpOnly防脚本攻击# secure:在Cookie中标记该变量表明只有当浏览器和Web Server之间的通信协议为加密认证协议时# # {domain: .gonggaotong.net, httpOnly: False, name: Hm_lpvt_5aed315e6cf23667dff3f1224c5dcb60, path: /, secure: False, value: 1642657344}# 筛选cookieif bilibili.com in data[domain]:cookie_dict {data[name]: data[value], Domain: data[domain], Path: data[path], Expires: data[expires]}print(cookie_dict)return cookie_dictws_url hit_that_secret_json_path_like_its_1997() print(ws_url) data_list gimme_those_cookies(ws_url) print(data_list)cookie_dict_list [to_cookie_dict(data) for data in data_list]
遍历多个cookie字典将每个字典中的key和value格式化为keyvalue的字符串
cookie_str_list [] for cookie_dict in cookie_dict_list:if cookie_dict:for k, v in cookie_dict.items():cookie_str_list.append({}{}.format(k, v))# 使用;将多个keyvalue字符串连接在一起 cookie_str ;.join(cookie_str_list) print(cookie_str)获取到的Cookie
通过selenium获取cookie, requests携带cookie请求 先用selenium登录网站, 然后获取cookie, requests携带cookie访问 测试网站: http://exercise.kingname.info/exercise_login_success import json import requests import websocketGET_ALL_COOKIES_REQUEST json.dumps({id: 1, method: Storage.getCookies})def hit_that_secret_json_path_like_its_1997():response requests.get(http://127.0.0.1:9222/json)websocket_url response.json()[0].get(webSocketDebuggerUrl)return websocket_urldef gimme_those_cookies(ws_url):ws websocket.create_connection(ws_url)ws.send(GET_ALL_COOKIES_REQUEST)result ws.recv()ws.close()response json.loads(result)print(response)cookies response[result][cookies]return cookiesdef to_cookie_dict(data_list):cookie_dict {}for data in data_list:if kingname in data[domain]:cookie_dict[data[name]] data[value]return cookie_dictdef login(res):if not 登录成功 in res:print(未登录)else:print(已登陆)ws_url hit_that_secret_json_path_like_its_1997() data_list gimme_those_cookies(ws_url) cookie_dict to_cookie_dict(data_list) print(cookie_dict)# 一个是把cookie先写成字典形式然后把字典转换为cookiejar s requests.Session() # 开启一个会话Session res s.get(http://exercise.kingname.info/exercise_login_success) login(res.text)# requests.utils.cookiejar_from_dict 转换为cookiejar
requests.utils.dict_from_cookiejar 转换位字典
s.cookies requests.utils.cookiejar_from_dict(cookie_dict, cookiejarNone, overwriteTrue) res s.get(http://exercise.kingname.info/exercise_login_success) print(res.status_code) login(res.text)结果如下:
- 上一篇: 网畅学校网站管理系统wordpress 发通知
- 下一篇: 网店网络营销与推广策划书搜索引擎优化岗位
相关文章
-
网畅学校网站管理系统wordpress 发通知
网畅学校网站管理系统wordpress 发通知
- 技术栈
- 2026年04月20日
-
网博士自助建站系统阿里云购买域名后怎么建网站
网博士自助建站系统阿里云购买域名后怎么建网站
- 技术栈
- 2026年04月20日
-
网wordpress站底部图片悬浮广告传媒公司名字大全最新
网wordpress站底部图片悬浮广告传媒公司名字大全最新
- 技术栈
- 2026年04月20日
-
网店网络营销与推广策划书搜索引擎优化岗位
网店网络营销与推广策划书搜索引擎优化岗位
- 技术栈
- 2026年04月20日
-
网店营销策划方案范文郑州seo线上推广技术
网店营销策划方案范文郑州seo线上推广技术
- 技术栈
- 2026年04月20日
-
网店装修图片大全什么叫seo推广
网店装修图片大全什么叫seo推广
- 技术栈
- 2026年04月20日
