Skip to main content

一个使用pyppeteer对指定的请求url进行渲染的库


安装

pip install smawe-pyppeteer

快速开始

from smawe_pyppeteer import get, run, goto, logger

if __name__ == '__main__':
    # 写法一
    from pathlib import Path

    async def main():
        script = "document.cookie"
        r = await get(
            "http://www.fangdi.com.cn/index.html", auto_close=True, delay=5, pretend=True, headless=True,
            user_data_dir=Path("./test").resolve(),
            args=[], script=script, callable=handle,
            enabled_interception=True, enabled_maximize=False, cookies={"name": "k1", "value": "v1"}
        )
        print(r.text)
        print(r.request)
        print(r.status)
        print(r.script_result)
        print(r.cookies)

    async def handle(request):
        print(request.url)
        await request.continue_()

    run(main())

    logger.debug("=" * 50)

    # 写法二
    # 等价于r = run(get(*args, **kwargs))
    r = goto("http://www.fangdi.com.cn/index.html", pretend=True, delay=2)
    print(r.text)

API参考

  • async def smawe_pyppeteer.get( url, delay=None, wait_for=None, page_width=None, page_height=None, enabled_interception=None, script=None, callable=None, cookies=None,**kwargs ):
      使用pyppeteer对指定url进行渲染
  • param url: url
  • param wait_for: css选择器或者xpath, 等待指定的元素出现在页面上
  • param delay: 打开指定url后要等待的秒数, 默认为None不进行等待(同时指定了delay和wait_for,则先delay,然后再wait_for)
  • param page_width: 页面宽度
  • param page_height: 页面高度
  • param enabled_interception: 是否启用请求拦截
  • param callable: 启动了请求拦截后要调用的协程函数, 它接受单个request参数, 未提供则使用默认实现.
  • param script: js表达式/js函数, 脚本在最后才执行, 即page关闭前执行. 结果可通过PyppeteerResponse.script_result属性获取.
  • param cookies: 一个字典或者包含字典的可迭代对象, 在当前打开的page中设置cookie(在访问页面之前设置), e.g. {"name": "n1", "value": "v1"}
  • param kwargs(包含以下关键字参数):
    • headless(bool): 是否启动无头模式, 默认是True.(启用了此参数(参数被设置为True时), auto_close被强制设为True)
    • path(str): 要运行的 Chromium 或 Chrome 可执行文件的路径.
    • auto_close(bool): 脚本完成后自动关闭浏览器进程. 默认为 True.
    • window_width(str): 浏览器窗口宽度, 以像素为单位.注意window_width和window_height必须同时设置, 否则会不生效
    • window_height(str): 浏览器窗口高度, 以像素为单位.注意window_width和window_height必须同时设置, 否则会不生效
    • args(list[str]): 传递给浏览器进程的附加参数.
    • enabled_maximize(bool): 是否启用窗口最大化, 默认为True, 如果同时指定了注意window_width和window_height, 此参数不会生效
  • return: PyppeteerResponse
  • smawe_pyppeteer.run(f: Coroutine)
      运行future对象(包括协程对象), 返回future的结果

  • smawe_pyppeteer.goto(*args, **kwargs)
    参数同smawe_pyppeteer.get, 等同于调用run(get(*args, **kwargs)), 返回一个PyppeteerResponse.

  • smawe.logger
    logging.Logger对象, 调用logger.debug("message""), 输出格式如下
    2023-11-15 22:51:12,342 | DEBUG | message


有问题请提交issue

Metadata

Release files for smawe-pyppeteer 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for smawe-pyppeteer 0.1.0
File Size Uploaded Provenance Actions
smawe_pyppeteer-0.1.0.tar.gz 10.2 kB Without provenance Details for smawe_pyppeteer-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for smawe-pyppeteer 0.1.0
File Interpreter ABI Platform Actions
smawe_pyppeteer-0.1.0-py3-none-any.whl Python 3 none any Details for smawe_pyppeteer-0.1.0-py3-none-any.whl

Total release size: 23.0 kB

Release files / smawe_pyppeteer-0.1.0.tar.gz

Download URL smawe_pyppeteer-0.1.0.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
84a851ee23398f9e9e5f2e8ff9a4fa0422a6f4166f589b402a695f2346b6f7ac
BLAKE2b-256 checksum
How to use checksums
e3c2209a1b41b7c54159793fb50752669ce3ef7dcc0bf7a1cccc0862595244e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.7

Release files / smawe_pyppeteer-0.1.0-py3-none-any.whl

Download URL smawe_pyppeteer-0.1.0-py3-none-any.whl
Size 12.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5e9095d7e9b6d0183ade86637880b5cdf44e7aced356f959cd5b2775cc71c530
BLAKE2b-256 checksum
How to use checksums
06e71d047f6bb0751b4ddfb7d3c2c0bc3924f9ec778869e038d77bd7e06dc124
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.7

Security for smawe-pyppeteer 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page