V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
heartlocker
V2EX  ›  Python

Python GUI 自动化框架 Clicknium

  •  
  •   heartlocker · 2022-08-02 14:45:44 +08:00 · 800 次点击
    这是一个创建于 626 天前的主题,其中的信息可能已经有所发展或是发生改变。

    看到有不少问爬虫 Playwright,Selenium 的问题。 给大家介绍一个新的 Python 自动化框架 Clicknium 。Clicknium[https://www.clicknium.com/documents]的优势在于使用特别简单,同时支持自动化桌面 app 和 Web 。 目前刚起步,欢迎大家多提意见和需求。

    我用一个自动下载 Youtube 视频的 Sample 介绍一下使用方法。

    准备

    • Windows 机器(不支持 Mac )
    • Python 3.8+
    • VS Code
    • 梯子

    安装

    pip install clicknium
    pip install pytube
    

    配置 Clicknium

    1. 在 VS Code 中安装 Clicknium 插件并登录(是的,只支持 VS Code )

    2. 安装 Chrome 插件

    安装 VS Code 插件

    安装 chrome 插件

    安装 VS Code 后再拓展商店搜索 Clicknium ,安装以后注册登录,然后 VS Code 左侧会出现一个奇怪的 logo 。 安装好 Chrome 插件后记得启用, 找不到的在浏览器地址栏输入 chrome://extensions/。

    Code

    用 VS Code 创建一个 Python 文件,比如 youtube.py

    from clicknium import clicknium as cc, locator
    
    def main():
        tab = cc.chrome.open("https://www.youtube.com")
    
    if __name__ == "__main__":
        main()
    

    可以通过 F5 运行。 这行代码 Clicknium 会自动打开 Chrome 浏览器,并且进入油管的首页。

    抓取

    这个时候,我们要下载霉霉的视频, 那就得先打开霉霉视频页,类似把大象塞进冰箱:

    步骤:

    1. 在搜索框输入 Taylor Swift
    2. 点击搜索按钮
    3. 点击进入霉霉首页
    4. 点击进入霉霉视频页

    这里一个涉及到四个元素:搜索框, 搜索按钮,搜索结果中的艺人名字,视频页切换按钮

    Clicknium 中使用 Locator 来定位 UI 元素, 并且提供了 Recorder 来生成 Locator 。

    我们使用上面的代码打开油管页面然后打开 VS Code 调用 Recorder 。

    调用 Recorder

    点击上图 VS Code 中 Locator tab 上这个小小的 capture 按钮启用 Recorder 。这个按钮比较隐蔽( Good job !)如果没看到 LOCATORS 这个 tab ,点击右上角的三个点勾选 Locator 。 将鼠标移动到搜索栏上,会自动高亮显示 input 。按住 Ctrl+Click(鼠标右键单击)即可抓取 搜索框。 同样的方法 抓取搜索按钮,和下图右上角Taylor Swift 的链接

    Youtube 页

    Recorder

    每次抓取都会在 Recorder 中生成一个 UI 元素对应的 locator ,可以对其进行重命名。完成后点击 Complete 。

    然后回到 VS Code

    from clicknium import clicknium as cc, locator
    from clicknium.common.enums import *
    
    def main():
        urlArrary = []
        tab = cc.chrome.open("https://www.youtube.com")
        tab.find_element(locator.chrome.youtube.searchBar).set_text(
            "Taylor Swift", by='sendkey-after-click')
        tab.find_element(locator.chrome.youtube.button_search_icon_legacy).click()
        tab.find_element(locator.chrome.youtube.TS).click()
    
    if __name__ == "__main__":
        main()
    

    通过 find_element 函数传入对应 locator 定位到某个 UI 元素然后利用 set_text 方法将“Taylor Swift”写入搜索框。

    下一行是相同的方法定位到搜索按钮,然后用 click 函数表示鼠标点击。TS 表示搜索结果中 taylor Swift 的连接。

    运行上面的 code ,进入霉霉的主页,我们采用相同的方法进入视频列表。

    视频列表

    在上图的列表中我们需要拿到每个视频的地址。 这个地址可以通过 locator 取得。 我们不可能给每个视频都抓取一个 Locator ,这里使用 Clicknium Recorder 一个非常强大的功能 Similar elements 。点击下图的按钮后, 同样采用 Ctrl+Click 的方式,Clicknium 就能自动识别的到类型的元素,生成一个 locator 。

        tab = cc.chrome.open("https://www.youtube.com")
        tab.find_element(locator.chrome.youtube.searchBar).set_text(
            "Taylor Swift", by='sendkey-after-click')
        tab.find_element(locator.chrome.youtube.button_search_icon_legacy).click()
        tab.find_element(locator.chrome.youtube.TS).click()
        tab.find_element(locator.chrome.youtube.div_video).click()
        tab.wait_appear(locator.chrome.youtube.a_video_title)#等待视频列表加载完毕
        vidioTitles = tab.find_elements(locator.chrome.youtube.a_video_title)
    

    因为视频列表是异步加载的, 我们需要使用 wait_appear 等待 locator 出现。因为使用 similar elements 抓取了多个视频,这个 locator 指向了多个视频所以使用 find_elements 方法。这个方法会返回一个 UiElement 的 List 。我们可以 href 从中获取到视频的相对路径,拼接上油管的地址就能得到完整的 URL 。

    有了完整 url ,就可以使用 Pytube 下载视频了。Pytube 可以根据指定的参数下载不同分辨率的视频,需要注意高画质是 video codec 和 audio codec 分开的。 具体可以参考Working with Streams and StreamQuery。 这里我们下载 1080p 的版本,下载路径可以修改 SAVE_PATH 。可是使用相同的方法实现视频上传。 反响好我就再写一篇。

    下面是完整的代码:

    from pytube import YouTube
    from clicknium import clicknium as cc, locator
    from clicknium.common.enums import *
    
    def downloadVideo(url):
        SAVE_PATH = "C:\\Users\\zhaoritian\\Downloads\\Youtube"
        try:
            yt = YouTube(url)
            yt.streams.filter(res="1080p").first().download(output_path=SAVE_PATH)
        except:
            print("Connection Error")  # to handle exception
    
        # filters out all the files with "mp4" extension
        print('Task Completed!')
    
    
    def main():
        urlArrary = []
        tab = cc.chrome.open("https://www.youtube.com")
        tab.find_element(locator.chrome.youtube.searchBar).set_text(
            "Taylor Swift", by='sendkey-after-click')
        tab.find_element(locator.chrome.youtube.button_search_icon_legacy).click()
        tab.find_element(locator.chrome.youtube.TS).click()
        tab.find_element(locator.chrome.youtube.div_video).click()
        tab.wait_appear(locator.chrome.youtube.a_video_title)
        videoTitles = tab.find_elements(locator.chrome.youtube.a_video_title)
        for locat in videoTitles:
            url = "https://www.youtube.com" + locat.get_property("href")
            urlArrary.append(url)
        tab.close()
    
        for v in urlArrary:
            downloadVideo(v)
    
    
    if __name__ == "__main__":
        main()
    

    参考:

    Clicknium

    Pytube

    4 条回复    2022-08-08 11:41:20 +08:00
    czwstc
        1
    czwstc  
       2022-08-03 12:04:43 +08:00
    安装依赖 pythonnet 的时候报错了:
    Installing collected packages: pythonnet, clicknium
    Running setup.py install for pythonnet ... error
    error: subprocess-exited-with-error

    × Running setup.py install for pythonnet did not run successfully.
    │ exit code: 1
    ╰─> [6 lines of output]
    usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
    or: setup.py --help [cmd1 cmd2 ...]
    or: setup.py --help-commands
    or: setup.py cmd --help

    error: option --single-version-externally-managed not recognized
    [end of output]

    note: This error originates from a subprocess, and is likely not a problem with pip.
    error: legacy-install-failure

    × Encountered error while trying to install package.
    ╰─> pythonnet

    note: This is an issue with the package mentioned above, not pip.
    hint: See above for output from the failure.
    czwstc
        2
    czwstc  
       2022-08-03 12:15:37 +08:00
    换了 3.8 可以,3.9 不行
    heartlocker
        3
    heartlocker  
    OP
       2022-08-03 13:54:27 +08:00
    @czwstc 3.9 需要 pythonnet 依赖。
    # Python version is 3.8 or below
    pip install clicknium

    # Python version is 3.9 or above
    pip install --pre pythonnet
    pip install clicknium
    heartlocker
        4
    heartlocker  
    OP
       2022-08-08 11:41:20 +08:00
    @czwstc 我更新了一版 现在 3.9+的版本也不需要安装前置依赖了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4620 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 10:02 · PVG 18:02 · LAX 03:02 · JFK 06:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.