Playwright使用pyinstaller进行打包,以及下载文件处理
Roy Lv7

pyinstaller 打包

一直使用 pipenv 虚拟环境管理器,在创建自动化任务移植到其他电脑环境进行使用的时候,需要📦为exe程序,但是不知道如何调用 Playwrightwebdriver
看到百度上没有什么可用的信息,在 Playwright 官方github 一个网友提供的解决方案可以完美解决这个问题。

记录一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from sys import modules
from os import listdir, path
from pathlib import Path
from typing import Union
from playwright import sync_api

#获取webdriver的路径地址,下面是chromium的,对应的webkit和firefox需要对应的更改路径,webkit在上层路径
def get_executable_path() -> Union[str, None]:
parent_folder = Path(modules['playwright'].__file__).parent / 'driver' / 'package' / '.local-browsers'

if not path.exists(parent_folder):
return None

child_folders = [name for name in listdir(parent_folder) if path.isdir(parent_folder / name) and name.strip().lower().startswith('chromium')]

if len(child_folders) != 1:
return None

chromium_folder = child_folders[0]

return parent_folder / chromium_folder / 'chrome-win' / 'chrome.exe'


with sync_api.sync_playwright() as p:
executable_path = get_executable_path()

if executable_path:
browser = p.chromium.launch(
headless=False,
executable_path=executable_path
)
page = browser.new_page()
page.goto("http://playwright.dev")
print(page.title())
browser.close()

调用和打包

CALL pip install playwright pyinstaller
CALL SET PLAYWRIGHT_BROWSERS_PATH=0
CALL playwright install chromium
CALL pyinstaller -y -F main.py –add-data “venv/lib/site-packages/playwright/driver;playwright/driver”

下载处理

查看官方的 Python 版本文档对于下载的调用写的不太清晰。
download.suggested_filename 这个接口,可能会因使用的 driver 不同获取到的下载文件名不同。文件名通常由浏览器根据 Content-Disposition 响应标头或下载属性计算得出。具体参见 whatwg 协议

In some cases, resources are intended for later use rather than immediate viewing. To indicate that a resource is intended to be downloaded for use later, rather than immediately used, the download attribute can be specified on the a or area element that creates the hyperlink to that resource.

The attribute can furthermore be given a value, to specify the filename that user agents are to use when storing the resource in a file system. This value can be overridden by the Content-Disposition HTTP header’s filename parameters. [RFC6266]

In cross-origin situations, the download attribute has to be combined with the Content-Disposition HTTP header, specifically with the attachment disposition type, to avoid the user being warned of possibly nefarious activity. (This is to protect users from being made to download sensitive personal or confidential information without their full understanding.)

正常情况下 download.suggested_filename 是可以获取到下载的文件名的。
但是如果需要将文件存储到特定位置在调用 download.save_as(path) 这个接口的时候,官方文档写的是 Path where the download should be copied. 也可能是我理解有问题,我直接将存储路径存储传给这个接口,发现不能正常执行。最终发现需要将完整的包含文件名的路径放作为path 才可以。
btw download.suggested_filename 可以用来判断文件是否下载完成。

  • 本文标题:Playwright使用pyinstaller进行打包,以及下载文件处理
  • 本文作者:Roy
  • 创建时间:2021-10-14 09:09:01
  • 本文链接:https://www.yrzdm.com/2021/10/14/playwright-pyinstaller/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!