ClericPy Ichrome Versions Save

Chrome controller for Humans, based on Chrome Devtools Protocol(CDP) and python3.7+.

4.0.1

6 months ago
  1. add webapp route for /request_get to send get request with headers,proxy
# ======================= 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&amp;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>"

4.0.0

6 months ago
  1. add retry for clear_dir_with_shutil
  2. add shutdown_reason for daemon
  3. use ruff-format instead of yapf
  4. add tab.run_js_snippets
  5. remove ichrome.sync. new sync mode with ichrome.debugger. #139 https://github.com/ClericPy/ichrome/blob/master/examples_debug.py
  6. rm null main_data_dir if _use_port_dir

3.2.0

11 months ago
  • add Tab.info, Tab.get_info, Tab.new_tab, Tab.browserContextId, Tab.get_targets

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())


3.1.1

1 year ago
  • fix missing tab0 while start_url is null, now default start_url is "about:blank"
  • add ChromeDaemon.cleanup_launched_pids to atexit to kill all the chrome processes which ichrome launched.
    • kill the chrome processed which not exiting
  • update tab.iter_fetch methods
    • now tab.iter_fetch will contain networkId to get response body while requestStage is Response.
    • add get_response for events
    • add match_event to filt the events with RequestPattern
  • new version chrome forbidden GET method towards /json/new fixed #123

3.1.0

1 year ago
  • add tab.scrollIntoView
  • Merge pull request #118 from why2lyj/master
    • add tab.set_geolocation_override
  • EventBuffer.events type add Dict[str, Callable], Bind different callback functions for different events

3.0.9

1 year ago

3.0.7

1 year ago
  • add edge path for linux mac
  • add Tab._default_recv_callback for sync_utils

3.0.4

1 year ago
  • add Daemon._iter_chrome_path
  • fix http preview bad encoding
  • CTRL-C can not stop daemons of ChromeWorkers fix #95

3.0.3

1 year ago

3.0.2 ~ 3.0.3

  • add kwargs & callback for EventBuffer fixed #89
  • add captureBeyondViewport for screenshot
  • pool Use incognico mode by default, or you can se ChromeEngine.DEFAULT_INCOGNITO_ARGS = None to use normal mode
  • different hosts of chrome processes are killed by the same port #94 fixed
  • Not Support edge browser #92 fix
  • fix Identifier 'node' has already been declared
  • ChromeRuntimeError(f'[closed] {self} ws has been closed') fix

3.0.1

2 years ago
  • Tab._DEFAULT_FLATTEN=True by default
  • add Listener._SINGLETON_EVENT_KEY skip raising error
  • EventBuffer._SINGLETON_EVENT_KEY avoid duplicated event listen
  • fix set_url makes Event key duplicated error