【原创实践】python版playwright截取多个图

你可以使用 Playwright 来获取当前页面的截图。以下是一个示例代码:

复制代码
import os
import sys
from playwright.sync_api import sync_playwright


def find_chrome_executable():
    """
    自动检测 Chrome / Chromium 在 Windows / macOS / Linux 平台的常见安装位置。
    找不到返回 None。
    """

    if sys.platform.startswith("win"):
        paths = [
            os.path.expandvars(r"%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe"),
            os.path.expandvars(r"%ProgramFiles%\Google\Chrome\Application\chrome.exe"),
            os.path.expandvars(r"%LocalAppData%\Google\Chrome\Application\chrome.exe"),
        ]

    elif sys.platform.startswith("darwin"):  # macOS
        paths = [
            "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
            "/Applications/Chromium.app/Contents/MacOS/Chromium",
        ]

    elif sys.platform.startswith("linux"):
        paths = [
            "/usr/bin/google-chrome",
            "/usr/bin/google-chrome-stable",
            "/usr/bin/chromium",
            "/usr/bin/chromium-browser",
            "/snap/bin/chromium",
        ]

    else:
        return None

    for path in paths:
        if os.path.exists(path):
            return path

    return None


def click_first_link_in_hotsearch_and_get_url():
    chrome_path = find_chrome_executable()

    if not chrome_path:
        print("❌ 未找到本地 Chrome,程序退出。")
        return

    print(f"✔ 使用本地 Chrome:{chrome_path}")

    with sync_playwright() as p:
        # 不允许 fallback 用内置 Chromium
        browser = p.chromium.launch(executable_path=chrome_path, headless=True)

        page = browser.new_page()
        page.goto('https://www.baidu.com')
        page.wait_for_load_state('load')

        hotsearch_selector = '#s-hotsearch-wrapper'
        page.wait_for_selector(hotsearch_selector)

        hotsearch_element = page.query_selector(hotsearch_selector)

        if hotsearch_element:
            first_link = hotsearch_element.query_selector("a")

            if first_link:
                url = first_link.get_attribute('href')
                print(f"第一个链接URL:{url}")

                page.goto(url)
                page.wait_for_load_state('load')

                screenshot_path = 'screenshot_after_click.png'
                page.screenshot(path=screenshot_path)

                print(f"截图已保存到 {screenshot_path}")
            else:
                print("热搜区未找到链接")
        else:
            print("页面上未找到热搜元素")

        browser.close()


if __name__ == '__main__':
    click_first_link_in_hotsearch_and_get_url()

在这个示例中,page.screenshot() 方法用于获取当前页面的截图,并将其保存到指定的文件路径(screenshot.png)。确保替换 'https://www.baidu.com' 为你想要截图的实际网址。如果你希望在截图后不关闭浏览器,记得注释掉 browser.close() 部分。

注意:截图之前最好等待页面加载完成,以确保获取到完整的页面截图。在这个示例中,使用了 page.wait_for_load_state('load') 来等待页面加载完成,但你可能需要根据实际情况调整等待的条件。

相关推荐
哥不想学算法5 小时前
【C++】字符串字面量拼接
开发语言·c++
hhzz6 小时前
Python大数据实战(十六):音乐推荐系统——基于协同过滤算法构建个性化歌单引擎
大数据·人工智能·python·数据挖掘·数据分析
SunnyDays10117 小时前
Python PDF 转 Markdown 详解(转换整个文档,特定页面,表格,扫描 PDF)
python·pdf 转 markdown·pdf 转 md·扫描 pdf 转 md·pdf 表格转 md
gugucoding7 小时前
21. 【C语言】打包不同类型:结构体
c语言·开发语言
2601_954706497 小时前
云手机 API 自动化实战:Python 批量控机、挂机脚本完整实现
python·智能手机·自动化
Brookty7 小时前
【JavaEE】线程安全(一).4:写块串行保安全、CAS
java·开发语言·java-ee·多线程·线程安全
F20226974868 小时前
西门子 PLC 与 C# 通信
开发语言·c#
gugucoding8 小时前
31. 【C语言】堆栈与队列的实现
c语言·开发语言·数据结构·链表
万联WANFLOW9 小时前
多分支企业组网,IP 网段到底该怎么规划
开发语言·php