self_example/Spider/Chapter07_动态渲染页面爬取/playwrightLearning/demo3移动端浏览器.py

45 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- encoding:utf-8 -*-
'''
@Author : dingjiawen
@Date : 2023/12/7 14:40
@Usage :
@Desc :playwright还支持移动端浏览器
'''
import time
from playwright.sync_api import sync_playwright
# 模拟打开iPhone 12 Pro Max的safari浏览器
with sync_playwright() as p:
iphone_12_pro_max = p.devices['iPhone 12 Pro Max']
browser = p.webkit.launch(headless=False)
context = browser.new_context(
**iphone_12_pro_max,
locale='zh-CN',
)
page = context.new_page()
page.goto('https://www.whatismybrowser.com/')
# 等待页面的某个状态完成这里传入的state是networkidle表示网络空闲状态
page.wait_for_load_state(state='networkidle')
page.screenshot(path='browser-info.png')
time.sleep(10)
browser.close()
with sync_playwright() as p:
iphone_12_pro_max = p.devices['iPhone 12 Pro Max']
browser = p.webkit.launch(headless=False)
context = browser.new_context(
**iphone_12_pro_max,
locale='zh-CN',
geolocation={'longitude': 116.39014, 'latitude': 39.913904},
permissions=['geolocation']
)
page = context.new_page()
page.goto('https://amap.com')
page.wait_for_load_state(state='networkidle')
page.screenshot(path='location-iphone.png')
time.sleep(10)
browser.close()