美文网首页
python+playwright

python+playwright

作者: 粥一样温柔 | 来源:发表于2023-10-29 20:32 被阅读0次

    一:用python+playwright对比网页图片和本地图片

    百度图片.png
    from playwright.sync_api import Playwright, sync_playwright
    import base64
    
    # 初始化Playwright
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)
        page = browser.new_page()
    
        # 打开网页
        page.goto('https://www.baidu.com')
    
        # 获取图片url
        image_url = page.locator('#s_lg_img_new')
        image_url = image_url.get_attribute('src')
    
        #将图片转成base64
        image_data_base64 = page.evaluate('''(image_url) => {
            return fetch(image_url)
                .then(response => response.arrayBuffer())
                .then(buffer => {
                    const byteArray = new Uint8Array(buffer);
                    return btoa(String.fromCharCode.apply(null, byteArray));
                });
        }''', image_url)
    
        # Base64エンコードされた画像データをデコードしてファイルに保存または他の操作を行うことができます
        # 把base64的数据转成dyte数组
        image_data_bytes = base64.b64decode(image_data_base64)
        def test_1():
            # 与本地图片比较,判断是否一致
            with open('baidu.png', 'rb') as localFile:
                assert localFile.read() == image_data_bytes
        browser.close()
    
    

    二:用page.on('response', data => {});方法监听,获取图片url,比对图片

    import base64
    
    from playwright.sync_api import sync_playwright
    
    
    def image_compare(response):#image_compare(response)方法,遍历response的url,查看是否存在图片
        if '1771365ca98ig9er706.jpg' in response.url:
            with open(r'./1771365ca98ig9er706.jpeg', 'rb') as localFile:
                print(localFile.read() == response.body())
    
    # Playwrightの初期化
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)
        page = browser.new_page()
    
        # 监听网页的request
        #page.on('request', lambda request: print(request.url, request))
        # 监听和遍历网页的response,每次遍历都调用image_compare()方法,将response作为参数传入方法
        page.on('response', lambda response: image_compare(response))
    
        # 打开网页
        page.goto('https://www.douban.com/')
    
    

    三:用page.on()和open()方法下载图片

    from playwright.sync_api import sync_playwright
    import base64
    
    # image_compare(response)方法,遍历response的url,查看是否存在图片
    def image_download(response):
        if '1771365ca98ig9er706.jpg' in response.url:
            # with open(r'./1771365ca98ig9er706.jpeg', 'rb') as localFile:
            #     print(localFile.read() == response.body())
            with open('./downloaded_doubanban.png', 'wb') as image_file:
                image_file.write(response.body())
                print('画像データを取得しました')
    
    
    # Playwrightの初期化
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)
        page = browser.new_page()
    
        # 监听网页的request
        # page.on('request', lambda request: print(request.url, request))
        # 监听和遍历网页的response,每次遍历都调用image_compare()方法,将response作为参数传入方法
        page.on('response', lambda response: image_download(response))
    
        # 打开网页
        page.goto('https://www.douban.com/')
    
    

    四:用base46转成字节文件

    from playwright.sync_api import sync_playwright
    import base64
    
    # Playwrightの初期化
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page()
    
        # ウェブページにアクセス
        page.goto('https://www.example.com')  # 画像を取得したいページのURLに変更
    
        # 画像のURLを指定
        image_url = 'https://www.example.com/path/to/hokkaido.png'  # 画像のURLに変更
    
        # ウェブページ内でJavaScriptを実行して画像データを取得
        image_data_base64 = page.evaluate('''(image_url) => {
            return fetch(image_url)
                .then(response => response.arrayBuffer())
                .then(buffer => {
                    const byteArray = new Uint8Array(buffer);
                    return btoa(String.fromCharCode.apply(null, byteArray));
                });
        }''', image_url)
    
        if image_data_base64:
            # Base64エンコードされた画像データをデコードしてファイルに保存または他の操作を行うことができます
            image_data_bytes = base64.b64decode(image_data_base64)
            with open('./downloaded_image.png', 'wb') as image_file:
                image_file.write(image_data_bytes)
    
            print('画像データを取得しました')
        else:
            print('画像データの取得に失敗しました')
    
        # ブラウザを閉じる
        browser.close()
    

    相关文章

      网友评论

          本文标题:python+playwright

          本文链接:https://www.haomeiwen.com/subject/rwfuidtx.html