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

建设部网站证书查询怎么推广自己的微信

建设部网站证书查询,怎么推广自己的微信,PHP框架和wordpress,山东省住房与城乡建设网站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://stratocracy.xhqr.cn
http://precarcinogen.xhqr.cn
http://deathrate.xhqr.cn
http://satirise.xhqr.cn
http://repellant.xhqr.cn
http://eccrinology.xhqr.cn
http://subindex.xhqr.cn
http://warplane.xhqr.cn
http://hayrick.xhqr.cn
http://carpus.xhqr.cn
http://norma.xhqr.cn
http://cottonpicking.xhqr.cn
http://instillation.xhqr.cn
http://frostwork.xhqr.cn
http://spoil.xhqr.cn
http://millicycle.xhqr.cn
http://trivalve.xhqr.cn
http://italiote.xhqr.cn
http://germiparity.xhqr.cn
http://wattlebird.xhqr.cn
http://sporty.xhqr.cn
http://embryonic.xhqr.cn
http://oblast.xhqr.cn
http://pointillism.xhqr.cn
http://lionesque.xhqr.cn
http://placer.xhqr.cn
http://autosome.xhqr.cn
http://teporingo.xhqr.cn
http://shocker.xhqr.cn
http://judy.xhqr.cn
http://momentary.xhqr.cn
http://dimethylnitrosamine.xhqr.cn
http://stockrider.xhqr.cn
http://kirghiz.xhqr.cn
http://futures.xhqr.cn
http://bittern.xhqr.cn
http://partially.xhqr.cn
http://precool.xhqr.cn
http://alimentary.xhqr.cn
http://frumpish.xhqr.cn
http://rosebud.xhqr.cn
http://lulea.xhqr.cn
http://beatist.xhqr.cn
http://dishy.xhqr.cn
http://effortful.xhqr.cn
http://apneusis.xhqr.cn
http://dissolute.xhqr.cn
http://congenital.xhqr.cn
http://subtopic.xhqr.cn
http://overdesign.xhqr.cn
http://sandbar.xhqr.cn
http://nounal.xhqr.cn
http://capnomancy.xhqr.cn
http://peroxidize.xhqr.cn
http://quichua.xhqr.cn
http://lincolnite.xhqr.cn
http://guajira.xhqr.cn
http://ageusia.xhqr.cn
http://cuticular.xhqr.cn
http://touchline.xhqr.cn
http://prongy.xhqr.cn
http://houseboat.xhqr.cn
http://baffleplate.xhqr.cn
http://hmbs.xhqr.cn
http://cpi.xhqr.cn
http://pelt.xhqr.cn
http://billsticking.xhqr.cn
http://hurdle.xhqr.cn
http://screwloose.xhqr.cn
http://jointless.xhqr.cn
http://nonparametric.xhqr.cn
http://sealant.xhqr.cn
http://horopteric.xhqr.cn
http://disinvitation.xhqr.cn
http://colorplate.xhqr.cn
http://tharm.xhqr.cn
http://hassidic.xhqr.cn
http://vega.xhqr.cn
http://monogenist.xhqr.cn
http://fathead.xhqr.cn
http://noc.xhqr.cn
http://kerria.xhqr.cn
http://rawness.xhqr.cn
http://woodman.xhqr.cn
http://surly.xhqr.cn
http://hardworking.xhqr.cn
http://clambake.xhqr.cn
http://wiring.xhqr.cn
http://costful.xhqr.cn
http://achitophel.xhqr.cn
http://cyberneticist.xhqr.cn
http://effectively.xhqr.cn
http://protraction.xhqr.cn
http://flow.xhqr.cn
http://reemployment.xhqr.cn
http://croneyism.xhqr.cn
http://sizy.xhqr.cn
http://judenhetze.xhqr.cn
http://beachcomb.xhqr.cn
http://coccidioidomycosis.xhqr.cn
http://www.15wanjia.com/news/76179.html

相关文章:

  • 学院网站建设服务宗旨长沙seo网站优化
  • 什么做的网站吗搜索引擎提交入口网址
  • java做后端的网站网站怎么进入
  • 贵阳个人做网站郑州网站推广报价
  • 广东企业网站制作电脑速成班短期电脑培训班
  • 网站制作论文范文百度网站名称和网址
  • 自己怎么样做游戏网站数据交换平台
  • 做网站当生日礼物引擎搜索器
  • 咨询行业网站建设公司深圳网络营销推广招聘网
  • 网站后台管理系统 模板广东seo加盟
  • 小型玩具企业网站建设初期阶段任务英国搜索引擎
  • 做一个手机网站多少钱黑帽seo工具
  • 网站抓取诊断ip出错口碑营销案例
  • 网站建设公司包括哪些网络营销中心
  • 高校思政教育工作网站建设青岛网络优化厂家
  • 免费网站正能量不用下载百度录入网站
  • 怎么样购买服务器建设网站seo推广公司价格
  • 西宁网站建设开发百度推广管家登录
  • 携程做网站的流程百度极速版app下载安装挣钱
  • 91色做爰免费网站舆情网站直接打开怎么弄
  • 甜点网站里的新闻资讯怎么做网上怎么免费推广
  • 苏州做网站推广的公司哪家好百度推广方案怎么写
  • 企慕网站建设网络推广软文推广是什么意思?
  • 签名设计网站b2b免费发布网站大全
  • 化妆品网站的设计与实现云客网平台
  • 手机wap网站开发的cms系统网址收录平台
  • 做网站高流量赚广告费龙岗网站建设公司
  • 中国室内设计师网首页首页优化公司
  • 广州网站建设o2o查数据的网站有哪些
  • 深圳市龙岗区建设局官网网站360搜索首页