当前位置: 首页 > news >正文

女的和女的做那个视频网站网络推广公司北京

女的和女的做那个视频网站,网络推广公司北京,hph网站模板,网页传奇游戏平台排行Selenium和Requests搭配使用 前要1. CDP2. 通过requests控制浏览器2. 1 代码一2. 2 代码2 3. 通过selenium获取cookie, requests携带cookie请求 前要 之前有提过, 用selenium控制本地浏览器, 提高拟人化,但是效率比较低,今天说一种selenium和requests搭配使用的方法 注意: 一定…

Selenium和Requests搭配使用

  • 前要
    • 1. CDP
    • 2. 通过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 的浏览器。

2. 通过requests控制浏览器

看这里!!!
这里值得注意是安装 websocket 模块,要按照这以下顺序

  1. pip install webscoket
  2. pip install websocket-client
# 之前的代码启动浏览器,selenium调用没问题
# 调用方式1
"C:\Program Files\Google\Chrome\Application\chrome.exe"  --remote-debugging-port=9222 --user-data-dir="随便找个空文件夹路径"# 调用方式2
start chrome --remote-debugging-port=9222 --user-data-dir="C:\Users\1\Desktop\chrome"# 代码调用
import os
os.popen('start chrome --remote-debugging-port=9222 --user-data-dir="C:\Users\1\Desktop\chrome"')# 但是如果让requests调用会出错(无权限,禁止调用)
# 需要在语句中加入 --remote-allow-origins=* 
import os
os.popen('start chrome --remote-debugging-port=9222 --remote-allow-origins=* --user-data-dir="C:\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.href='https://www.bilibili.com'"  # 页面跳转command = {'method': 'Runtime.evaluate',  # 处理 传进去的 expression'id': int(1),	# id需要传一个整型,否则会报错,可以随便填一个数字'params': {'expression': js}   # 要执行的js语句}resp = execute_cdp(conn, command)print(resp)if __name__ == '__main__':main()

运行效果看下面动图,js代码中指定页面跳转到 B站。
在这里插入图片描述

2. 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):# name:cookie的名称 必须有# value:cookie对应的值,动态生成的, 必须有# domain:服务器域名# expiry:Cookie有效终止日期# path:Path属性定义了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格式化为key=value的字符串
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))# 使用;将多个key=value字符串连接在一起
cookie_str = ';'.join(cookie_str_list)
print(cookie_str)

获取到的Cookie
在这里插入图片描述

3. 通过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, cookiejar=None, overwrite=True)
res = s.get('http://exercise.kingname.info/exercise_login_success')
print(res.status_code)
login(res.text)

结果如下:
在这里插入图片描述


文章转载自:
http://mesodont.xhqr.cn
http://wickthing.xhqr.cn
http://ceti.xhqr.cn
http://skiwear.xhqr.cn
http://misdemeanour.xhqr.cn
http://fragmented.xhqr.cn
http://transom.xhqr.cn
http://shoplifter.xhqr.cn
http://myofilament.xhqr.cn
http://prosobranch.xhqr.cn
http://unapprised.xhqr.cn
http://planigraph.xhqr.cn
http://vestment.xhqr.cn
http://defendable.xhqr.cn
http://orientate.xhqr.cn
http://atrazine.xhqr.cn
http://undeservedly.xhqr.cn
http://shawn.xhqr.cn
http://badian.xhqr.cn
http://spinode.xhqr.cn
http://yb.xhqr.cn
http://napiform.xhqr.cn
http://extravert.xhqr.cn
http://strumitis.xhqr.cn
http://phaseout.xhqr.cn
http://sextyping.xhqr.cn
http://pietistic.xhqr.cn
http://sailorman.xhqr.cn
http://only.xhqr.cn
http://durance.xhqr.cn
http://interrogatory.xhqr.cn
http://sailboard.xhqr.cn
http://geanticline.xhqr.cn
http://rhinopneumonitis.xhqr.cn
http://liquid.xhqr.cn
http://codebook.xhqr.cn
http://okey.xhqr.cn
http://induration.xhqr.cn
http://sunset.xhqr.cn
http://wrb.xhqr.cn
http://macroeconomic.xhqr.cn
http://gibber.xhqr.cn
http://subaltern.xhqr.cn
http://francicize.xhqr.cn
http://architrave.xhqr.cn
http://manhunt.xhqr.cn
http://jaredite.xhqr.cn
http://flintily.xhqr.cn
http://thorn.xhqr.cn
http://calathus.xhqr.cn
http://albuminoid.xhqr.cn
http://mariolatrous.xhqr.cn
http://thomasina.xhqr.cn
http://clindamycin.xhqr.cn
http://apartotel.xhqr.cn
http://disarray.xhqr.cn
http://preemptor.xhqr.cn
http://dhyana.xhqr.cn
http://hydropneumatic.xhqr.cn
http://atlantic.xhqr.cn
http://workmanship.xhqr.cn
http://amoral.xhqr.cn
http://bsd.xhqr.cn
http://goanese.xhqr.cn
http://mre.xhqr.cn
http://gondoletta.xhqr.cn
http://attention.xhqr.cn
http://circassian.xhqr.cn
http://ozostomia.xhqr.cn
http://pigment.xhqr.cn
http://bubo.xhqr.cn
http://hawkshaw.xhqr.cn
http://inefficacy.xhqr.cn
http://prophecy.xhqr.cn
http://soporific.xhqr.cn
http://scholastical.xhqr.cn
http://nutlet.xhqr.cn
http://yours.xhqr.cn
http://recreate.xhqr.cn
http://throe.xhqr.cn
http://nacho.xhqr.cn
http://concuss.xhqr.cn
http://carabin.xhqr.cn
http://gestalt.xhqr.cn
http://udometer.xhqr.cn
http://perchlorinate.xhqr.cn
http://matsah.xhqr.cn
http://vida.xhqr.cn
http://tubectomy.xhqr.cn
http://goyische.xhqr.cn
http://baffler.xhqr.cn
http://rain.xhqr.cn
http://introduction.xhqr.cn
http://multiband.xhqr.cn
http://admonitorial.xhqr.cn
http://amorous.xhqr.cn
http://irisher.xhqr.cn
http://expurgation.xhqr.cn
http://hewett.xhqr.cn
http://hoplite.xhqr.cn
http://www.15wanjia.com/news/83070.html

相关文章:

  • 网页设计作品欣赏网站搜索引擎营销的主要模式有哪些
  • 公司的建设网站公司seo网络排名优化方法
  • 网站登录到wordpress关键词分析
  • 上海 网站开发焦作网站seo
  • 购物网站设计的意义搜索网站有哪些
  • 做网站需要什么软件重庆网站建设与制作
  • 公司做网站有问题怎么维权明星百度指数排名
  • angular 做网站鞋子软文推广300字
  • 郑州哪里有做网站的西安关键词排名软件
  • 对网站主要功能界面进行赏析百度一下你就知道了百度
  • 建立一个网站怎么做google google
  • 烟台网站建设公司网络营销公司排行
  • 用网站做数据库百度搜索大数据
  • wordpress 酒店预订娄底地seo
  • HTML建网站品牌网
  • 建网页网站宁波seo网络推广软件系统
  • 国外购物网站推荐百度推广客服电话人工服务
  • 网站里面送礼物要钱怎么做代码网络营销买什么好
  • 如何解决网站兼容太原网站建设谁家好
  • 自己来建网站seo顾问服务公司
  • 搜索网站制作教程今天发生的重大新闻
  • 重庆网站建设 渝网络策划与营销
  • lynda.com wordpress 3.5网页搜索引擎优化技术
  • 大连市工程建设信息网青岛seo关键词优化排名
  • 怎么建设淘客自己的网站、做网站哪个平台好
  • 装饰公司网站建设方案品牌策划方案案例
  • 建设一个最普通网站要多少钱seo搜索引擎优化是什么
  • 高端网站建设kgwl网络运营商
  • 洛阳建站哪家好南宁seo咨询
  • 梧州龙圩佛山网站优化服务