Chrome 如何快速获取 request header?

2020-10-24 23:05:23 +08:00
 woshichuanqilz

一般的做法就是 f12 打开 dev tool 然后复制粘贴,

但是现在想用程序自动化的实现这个功能

我这边用了 mitmproxy 比较卡。

然后想到了 tampermonkey, 不太熟悉 js 没有找到办法, 这里请问一下如何能实现这个功能。 通过后台的程序自动化的获取 request header

2760 次点击
所在节点    Python
10 条回复
locoz
2020-10-24 23:19:25 +08:00
代理是最好的办法了…
ClericPy
2020-10-25 00:09:18 +08:00
```
import asyncio
from ichrome import AsyncChromeDaemon, AsyncTab


async def show_headers(tab: AsyncTab):
await tab.wait_response(filter_function=lambda r: 'httpbin.org/headers' in
r['params']['response']['url'],
callback_function=lambda r: print(
r['params']['response']['headers'], '\n', r[
'params']['response']['requestHeaders']))
# await tab.wait_request(lambda r: print(r))


async def main():
async with AsyncChromeDaemon() as cd:
async with cd.connect_tab() as tab:
task = asyncio.create_task(show_headers(tab))
await tab.goto('https://httpbin.org/headers')
# print(await tab.html)
await task


if __name__ == "__main__":
asyncio.run(main())


```
yucongo
2020-10-25 00:22:02 +08:00
自己解析解密 chrome 的 sqlite3 格式 cookies 库(可能比想象的稍微难一点)或是用 browser-cookie3 https://pypi.org/project/browser-cookie3/
crab
2020-10-25 00:30:57 +08:00
mengyx
2020-10-25 00:34:01 +08:00
我用做过一个 Golang 的 MITM Proxy 。性能还行,外包的项目中实际验证过,需要的话可以找我
mengyx
2020-10-25 00:37:15 +08:00
或者 Chrome 的 Devtools 也有相应 Api,https://chromedevtools.github.io/devtools-protocol/
ClericPy
2020-10-25 00:49:51 +08:00
V2 吞我空格...?

https://paste.ubuntu.com/p/QrDmcGwxvS/

加上 Headless 和禁用图片, 也可以屏蔽 css 和 mp4 没放上, 冷启动 3 秒, 连接已经启动的 tab 大概 1 秒(基本就是花在下载), 同域名并发被 Chrome 限制在 6 以内, 所以没写并发的
kajweb
2020-10-25 01:18:41 +08:00
我最近准备开源的 stop-debugger 您可以关注一下,基于 node 屏蔽浏览器调试 debugger 。
里面的 proxy-serve 模块可以满足您的要求。
基本原理是使用 net 模块获得请求并进行代理。

https://github.com/kajweb/stop-debugger
kajweb
2020-10-25 01:19:39 +08:00
浏览器拓展可以参考 XSwitch 的源码
woshichuanqilz
2020-10-25 11:12:24 +08:00
解决了谢谢各位的帮助, 主要参考的是 @Cleric 的思路, 我用了 pychrome

基本代码在这里比较粗糙

```
import pychrome
from urllib.parse import urlparse
import subprocess, signal
import os
import time


def killprocess(pname):
p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
out, err = p.communicate()
for line in out.splitlines():
pinfo = line.decode().lower()
if pname in pinfo:
pid = int(line.split(None, 1)[0])
os.kill(pid, signal.SIGKILL)

header = dict()
url = "https://www.dogedoge.com"
if not url.endswith('/'):
url += '/'
domain = urlparse(url).netloc
killprocess('chrome')
cmd = 'google-chrome-stable --remote-debugging-port=9222'
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
time.sleep(3)

# 创建一个浏览器实例
browser = pychrome.Browser(url="http://127.0.0.1:9222")

# 新建一个页签
tab = browser.new_tab()


# 需要注册的回调函数
def request_will_be_sent(**kwargs):
if url == kwargs.get('request').get('url'):
header = kwargs.get('request').get('headers')
return


tab.Network.requestWillBeSent = request_will_be_sent

# 开始接收消息, requestWillBeSent 事件发生时被注册的回调函数也可以执行
tab.start()

# 调用方法
tab.Network.enable()

# 调用方法并设置超时时间
tab.Page.navigate(url=url, _timeout=5)
input()
# 等待页面加载
tab.wait(5)

# 停止处理事件, 停止从 chrome 接收消息
tab.stop()

# 关闭页签
browser.close_tab(tab)

```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/718282

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX