Chrome controller for Humans, based on Chrome Devtools Protocol(CDP) and python3.7+.
# ======================= server code ===========================
from inspect import getsource
import requests
# 1. request_get demo
print(
requests.get(
"http://127.0.0.1:8009/chrome/request_get",
params={
"__url": "http://httpbin.org/get?a=1", # [required] target URL
"__proxy": "http://127.0.0.1:1080", # [optional]
"__timeout": "10", # [optional]
"my_query": "OK", # [optional] params for target URL
},
# headers for target URL
headers={
"User-Agent": "OK",
"my_header": "OK",
"Cookie": "my_cookie1=OK",
},
# cookies={"my_cookie2": "OK"}, # [optional] cookies for target URL if headers["Cookie"] is None
).text,
flush=True,
)
# <html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">{
# "args": {
# "a": "1",
# "my_query": "OK"
# },
# "headers": {
# "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
# "Accept-Encoding": "gzip, deflate",
# "Cookie": "my_cookie1=OK",
# "Host": "httpbin.org",
# "My-Header": "OK",
# "Upgrade-Insecure-Requests": "1",
# "User-Agent": "OK",
# "X-Amzn-Trace-Id": "Root=1-654d0664-74457b4317c2f41d17b6a823"
# },
# "origin": "1.1.1.1",
# "url": "http://httpbin.org/get?a=1&my_query=OK"
# }
# </pre></body></html>
# '"Herman Melville - Moby-Dick"'
# "<html><head><meta name=\"color-scheme\" content=\"light dark\"></head><body><pre style=\"word-wrap: break-word; white-space: pre-wrap;\">{\n \"origin\": \"103.171.177.94\"\n}\n</pre></body></html>"
# 2. test tab_callback
async def tab_callback(self, tab, data, timeout):
await tab.set_url(data["url"], timeout=timeout)
return (await tab.querySelector("h1")).text
r = requests.post(
"http://127.0.0.1:8009/chrome/do",
json={
"data": {"url": "http://httpbin.org/html"},
"tab_callback": getsource(tab_callback),
"timeout": 10,
},
)
print(repr(r.text), flush=True)
'"Herman Melville - Moby-Dick"'
async def tab_callback(task, tab, data, timeout):
await tab.wait_loading(3)
return await tab.html
# 3. incognito_args demo
print(
requests.post(
"http://127.0.0.1:8009/chrome/do",
json={
"tab_callback": getsource(tab_callback),
"timeout": 10,
"incognito_args": {
"url": "http://httpbin.org/ip",
"proxyServer": "http://127.0.0.1:1080",
},
},
).text
)
# "<html><head><meta name=\"color-scheme\" content=\"light dark\"></head><body><pre style=\"word-wrap: break-word; white-space: pre-wrap;\">{\n \"origin\": \"103.171.177.94\"\n}\n</pre></body></html>"
ichrome.sync
. new sync mode with ichrome.debugger. #139 https://github.com/ClericPy/ichrome/blob/master/examples_debug.py
Tab.new_tab demo
import asyncio
from ichrome import AsyncChromeDaemon
async def main():
async with AsyncChromeDaemon(headless=False, disable_image=True) as cd:
async with cd.incognito_tab() as tab:
url = 'http://www.bing.com/'
await tab.goto(url, timeout=3)
MUIDB = (await tab.get_cookies_dict([url])).get('MUIDB')
new_tab = await tab.new_tab()
tab_exist = bool(await tab.chrome.get_tab(new_tab.tab_id))
assert tab_exist
async with new_tab(auto_close=True) as tab:
# same context, so same cookie
MUIDB2 = (await tab.get_cookies_dict([url])).get('MUIDB')
# print(MUIDB, MUIDB2, MUIDB == MUIDB2)
assert MUIDB == MUIDB2
# the new_tab auto closed
tab_exist = bool(await tab.chrome.get_tab(new_tab.tab_id))
assert not tab_exist
asyncio.run(main())
ChromeDaemon.cleanup_launched_pids
to atexit to kill all the chrome processes which ichrome
launched.
tab.iter_fetch
methods
tab.iter_fetch
will contain networkId
to get response body while requestStage
is Response
.get_response
for eventsmatch_event
to filt the events with RequestPatternGET
method towards /json/new
fixed #1233.0.2 ~ 3.0.3
pool
Use incognico mode by default, or you can se ChromeEngine.DEFAULT_INCOGNITO_ARGS = None to use normal mode