playwright的使用
作者:互联网
1 安装playwright
# 安装
pip install playwright
# 安装 Chromium、Firefox 和 WebKit浏览器命令
playwright install
2 启动浏览器
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("http://playwright.dev")
print(page.title())
browser.close()
3 带参数和代理启动浏览器
chrome 参数说明网站 https://blog.csdn.net/mimishy2000/article/details/88315347
firefox 参数说明网站 https://blog.huati365.com/3ad5f64ade276769
from playwright.sync_api import sync_playwright
from playwright_stealth import stealth_sync
# playwright_stealth 这个第三方模块可执行
p = sync_playwright().start()
chrome_path = r"C:\Chrome\chrome.exe"
user_data = r"C:\test"
chrome_param = [
'--disable-popup-blocking',
'--disable-blink-features=AutomationControlled',
'--disable-bundled-ppapi-flash',
'--ignore-ssl-errors',
'--ignore-certificate-errors',
'--disable-gpu',
'--disable-webrtc-encryption',
'--disable_non_proxied_udp',
'lang=en-us'
]
proxy = {"server": "socks5://192.168.50.115:5000"}
browser = p.chromium.launch_persistent_context(headless=False,
args=chrome_param, # 浏览器参数列表 chrome,firefox,webkit都可以通过这个来设定参数
executable_path=chrome_path,# 设置浏览器的可执行文件
user_data_dir=user_data, # 设置浏览器缓存路径
user_agent="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
ignore_default_args=['--enable-automation'], # 这个可以直接看函数内的列表参数说明
chromium_sandbox=False, # 禁用chrome的方式
ignore_https_errors=True,
proxy=proxy # 设置代理
)
# 这里不要用new_page的方法 它在打开浏览器的时候会多打开新的标签页
page = browser.pages[0]
# page = browser.new_page()
stealth_sync(page)
page.goto('https://ip.voidsec.com/')
page.wait_for_timeout(600)
# 关闭浏览器
bbrowser.close()
p.stop()
4 火狐浏览器启动
from playwright.sync_api import sync_playwright
import time
from playwright_stealth import stealth_sync
user_data = r"C:\test_f"
firefox_path = r"C:\firefox\firefox.exe"
p = sync_playwright().start()
firefox_arg = [
"intl.accept_languages=zh-CN",
]
proxy = {"server": "socks5://192.168.50.115:5000"}
# 这里重新打开一次会出现打不开页面的情况 需要把firefox的缓存清掉在打开
browser = p.firefox.launch_persistent_context(headless=False, user_data_dir=user_data, executable_path=firefox_path,
ignore_https_errors=True,
args=firefox_arg # 此参数未生效
)
page = browser.pages[0]
stealth_sync(page)
page.goto("https://bot.sannysoft.com/")
page.wait_for_timeout(30000)
browser.close()
p.stop()
标签:playwright,firefox,--,sync,使用,page,browser 来源: https://www.cnblogs.com/harlot/p/15946801.html