本地playwright打包docker封装(firefox)
我的Dockerfile文件:
查看代码
FROM mcr.microsoft.com/playwright/python:v1.31.0
WORKDIR ./project
ADD . .
RUN pip install -r requirements.txt
ENTRYPOINT ["/bin/sh", "-c", "/usr/bin/xvfb-run -a $@", ""]
CMD ["xvfb-run", "python", "./src/tian_mao.py"]
执行命令:
查看代码
(base) kaka@KakadeMacBook-Pro project % docker build -t python/playwright_tianmao:v1.31.0 .
Sending build context to Docker daemon 22.02kB
Step 1/5 : FROM mcr.microsoft.com/playwright/python:v1.31.0
---> 7f3d74da0ce6
Step 2/5 : WORKDIR ./project
---> Using cache
---> 1b5215319b52
Step 3/5 : ADD . .
---> 9c48b9883b45
Step 4/5 : RUN pip install -r requirements.txt
---> Running in 87d000cfcfa0
Collecting playwright==1.31.1
Downloading playwright-1.31.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (34.9 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 34.9/34.9 MB 7.1 MB/s eta 0:00:00
Requirement already satisfied: typing-extensions in /usr/local/lib/python3.8/dist-packages (from playwright==1.31.1->-r requirements.txt (line 1)) (4.5.0)
Requirement already satisfied: pyee==9.0.4 in /usr/local/lib/python3.8/dist-packages (from playwright==1.31.1->-r requirements.txt (line 1)) (9.0.4)
Requirement already satisfied: greenlet==2.0.1 in /usr/local/lib/python3.8/dist-packages (from playwright==1.31.1->-r requirements.txt (line 1)) (2.0.1)
Installing collected packages: playwright
Attempting uninstall: playwright
Found existing installation: playwright 1.31.0
Uninstalling playwright-1.31.0:
Successfully uninstalled playwright-1.31.0
Successfully installed playwright-1.31.1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
Removing intermediate container 87d000cfcfa0
---> 8680d5167bef
Step 5/5 : CMD ["xvfb-run", "python", "./src/tian_mao.py"]
---> Running in d1e2e0b26ed6
Removing intermediate container d1e2e0b26ed6
---> 097f6ea38521
Successfully built 097f6ea38521
Successfully tagged python/playwright_tianmao:v1.31.0
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
(base) kaka@KakadeMacBook-Pro project %
(base) kaka@KakadeMacBook-Pro project % docker save -o playwright_tianmao.tar python/playwright_tianmao
然后运行看能不能起来:
查看代码
(base) kaka@KakadeMacBook-Pro project % docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python/playwright_tianmao v1.31.0 097f6ea38521 16 minutes ago 1.94GB
clickhouse/clickhouse-server <none> 854810a3ea35 3 months ago 705MB
redis alpine 7bf9ac7d3b84 4 months ago 28.1MB
mysql/mysql-server 8.0 55af6abb77a6 5 months ago 527MB
fp2_0-8.1/app latest 53b297f8c7a6 7 months ago 1.61GB
clickhouse/clickhouse-server latest d846490c0466 14 months ago 794MB
prom/prometheus latest ccf2871d6122 15 months ago 198MB
grafana/grafana-enterprise latest 05048bc21afe 15 months ago 290MB
prom/alertmanager latest 44a71f29f42b 18 months ago 55.3MB
(base) kaka@KakadeMacBook-Pro project % docker run -it --name=python_playwright -v /Users/kaka/miniconda3/envs/playwright_tianmao/project/src:/src python/playwright_tianmao:v1.31.0 /bin/bash
root@e07176c47338:/project# ls
Dockerfile requirements.txt src
进入到容器里面了,但是我exit退出了。回到docker界面:
查看代码
(base) kaka@KakadeMacBook-Pro ~ % docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e07176c47338 python/playwright_tianmao:v1.31.0 "/bin/bash" 2 minutes ago Exited (0) About a minute ago python_playwright
0371989e2ccb clickhouse/clickhouse-server:latest "/entrypoint.sh" 5 weeks ago Exited (0) 25 hours ago clickhouse
22d0abe7fe04 grafana/grafana-enterprise "/run.sh" 5 weeks ago Exited (0) 25 hours ago grafana
d7924246eb35 prom/prometheus "/bin/prometheus --c…" 5 weeks ago Exited (0) 25 hours ago prometheus
b6131d8861f1 prom/alertmanager "/bin/alertmanager -…" 5 weeks ago Exited (0) 25 hours ago altermanager
82c9f637311f fp2_0-8.1/app "start-container" 6 weeks ago Exited (255) 25 hours ago 6002/tcp, 8000/tcp, 0.0.0.0:80->8001/tcp fp20-fp2_0.test-1
3d682d5ba9e9 mysql/mysql-server:8.0 "/entrypoint.sh mysq…" 6 weeks ago Exited (255) 25 hours ago 0.0.0.0:3306->3306/tcp, 33060-33061/tcp fp20-mysql-1
0bc71d69051a redis:alpine "docker-entrypoint.s…" 3 months ago Exited (255) 25 hours ago 0.0.0.0:6379->6379/tcp fp20-redis-1
(base) kaka@KakadeMacBook-Pro ~ %
有这个容器了,再次运行:
查看代码
(base) kaka@KakadeMacBook-Pro ~ % docker start e07176c47338
e07176c47338
(base) kaka@KakadeMacBook-Pro ~ % docker exec -it e07176c47338 bash
root@e07176c47338:/project# ls
Dockerfile requirements.txt src
root@e07176c47338:/project# xvfb-run python src/tian_mao.py
快速进入->Exception:Timeout 2000ms exceeded.
=========================== logs ===========================
waiting for get_by_role("button", name="快速进入")
============================================================
未获取到登录的淘宝昵称->Exception:Timeout 5000ms exceeded.
=========================== logs ===========================
waiting for locator(".site-nav-bd > ul.site-nav-bd-l > li#J_SiteNavLogin > div.site-nav-menu-hd > div.site-nav-user > a.site-nav-login-info-nick")
============================================================
root@e07176c47338:/project#
进入了执行了python,但是没有获得想要的结果,查一下是什么原因哈哈哈:
查看代码
root@e07176c47338:/project# cat src/tian_mao.py
from playwright.sync_api import Playwright, sync_playwright, expect
# 对象初始化
def run(playwright: Playwright) -> None:
# 用户数据目录的路径,该目录存储浏览器会话数据,如 cookie 和本地存储。
user_data_dir = "/Users/kaka/firefox2"
# 传递给浏览器实例的附加参数。
args = [
'--disable-blink-features=AutomationControlled',
f"--disable-extensions",
f"--disable-popup-blocking",
f"--ignore-certificate-errors",
f"--disable-plugins-discovery",
f'--no-first-run',
f'--no-service-autorun',
f'--no-default-browser-check',
# f'--no-startup-window',
f'--disable-dev-shm-usage',
# f"--disable-extensions-except={cookie_extension},{path_to_extension}",
]
context = playwright.firefox.launch_persistent_context (
user_data_dir,
args=args,
bypass_csp=True,
channel="firefox",
device_scale_factor=1,
devtools=False,
headless=True,
)
page = context.new_page ()
# 登录网址
login_url = "https://login.taobao.com/member/login.jhtml"
# 打开网页
page.goto (login_url)
# 自适应等待,点击密码登录选项
page.set_default_timeout (2000)
# 账户名
try:
username = page.locator ('#fm-login-id')
username.click ()
username.fill ('账号')
except Exception as e:
print (f"账号名/邮箱/手机号->Exception:{e}")
pass
# 密码
try:
userpass = page.locator ('#fm-login-password')
userpass.click ()
userpass.fill ('密码')
except Exception as e:
print (f"请输入登录密码->Exception:{e}")
pass
# 获取拖动按钮位置并拖动
try:
drop_button = page.frame_locator ("#baxia-dialog-content").locator (
"#nc_1_n1z")
box = drop_button.bounding_box ()
page.mouse.move (box['x'] + box['width'] / 2, box['y'] + box['height'] / 2)
page.mouse.down ()
mov_x = box['x'] + box['width'] / 2 + 260
page.mouse.move (mov_x, box['y'] + box['height'] / 2)
page.mouse.up ()
except Exception as e:
print (f"获取拖动按钮位置并拖动->Exception:{e}")
pass
# 登录按钮
try:
# page.frame_locator("#J_taobao iframe").locator(".fm-submit").click()
page.get_by_role ("button", name="登录").click ()
except Exception as e:
print (f"登录按钮->Exception:{e}")
pass
# 快速进入
try:
# page.frame_locator("#J_taobao iframe").locator(".fm-submit").click()
c = page.get_by_role ("button", name="快速进入")
c.click (force=True)
c.click ()
except Exception as e:
print (f"快速进入->Exception:{e}")
pass
# html就是返回的页面源代码
html = page.content ()
# print (html)
# 直到获取到淘宝会员昵称才能确定是登录成功
page.set_default_timeout (5000)
try:
taobao_name = page.locator (
".site-nav-bd > ul.site-nav-bd-l > li#J_SiteNavLogin > div.site-nav-menu-hd > div.site-nav-user > a.site-nav-login-info-nick ")
print ('获取到登录的淘宝昵称是:' + str (taobao_name.inner_text ()))
except Exception as e:
print (f"未获取到登录的淘宝昵称->Exception:{e}")
pass
with sync_playwright () as playwright:
run (playwright)
root@e07176c47338:/project# python src/tian_mao.py
获取拖动按钮位置并拖动->Exception:Timeout 2000ms exceeded.
=========================== logs ===========================
waiting for frame_locator("#baxia-dialog-content").locator("#nc_1_n1z")
============================================================
快速进入->Exception:Timeout 2000ms exceeded.
=========================== logs ===========================
waiting for get_by_role("button", name="快速进入")
============================================================
获取到登录的淘宝昵称是:tb4210828_2012
root@e07176c47338:/project#
headless=True因为我把这个值改为了True所以,直接运行
python src/tian_mao.py
不在前面加
xvfb-run
哈哈哈,最后成功获得了,开心