playwright 应用
一 安装
pip install playwright pip install pytest-playwright playwright install #安装chouim
默认把浏览器驱动下载到:
C:\Users\xiake\AppData\Local\
C:\Users\xiake\AppData\Local\ms-playwright\chromium-1067\chrome-win
小案例1:
from playwright.sync_api import sync_playwright with sync_playwright() as p: brower =p.chromium.launch(headless=False,args=["--start-maxmized"],slow_mo=3000) page =brower.new_page(no_viewport=True) # 默认窗口大小失效 # page =brower.new_page(viewport={"width":1920,"height":1080}) # 默认窗口大小失效 page.goto("https://www.baidu.com") print(page.title()) page.get_by_text("百度一下").click() # 根据文本找到 page.go_back() # 后退 page.go_forward()# 前进 page.reload() # 刷新 brower.close()
小案例2:
from playwright.sync_api import sync_playwright playwright = sync_playwright().start() browser = playwright.chromium.launch(headless=False,slow_mo=2000) context = browser.new_context() page = context.new_page() page.goto("https://www.baidu.com") # $x() # xpath定位 #单属性定位 text1= page.locator("//span[@name='tj_settingicon']").text_content() print(text1) # 多属性定位 and # css定位 page.locator("#kw").fill("python") page.locator("#su").click() page.locator("#kw").fill("java") page.locator("input#kw").fill("java") # 标签加属性的方式 page.locator("#su").click() page.fill("//input[@id='kw']","你好啊") page.click("//input[@id='su']") browser.close()
截图:
#方式 1
page.screenshot(path="./1.png") # 长截图 self.page.screenshot(path="screenshot.png", full_page=True) # 截取单个元素 locator-定位器 page.locator("//input[@placeholder='手机号']").screenshot(path="./1.png")
小案例3:
from playwright.sync_api import sync_playwright import time class XiaoHongShu: def __init__(self): playwright = sync_playwright().start() self.browser = playwright.chromium.launch(headless=False) self.context = self.browser.new_context() # 第1个chromium # self.context2 = self.browser.new_context() # 第2个chromium self.page = self.context.new_page() # tab页 # self.page2 = self.context2.new_page() # tab页 def run(self): # page2 = context.new_page() # page2.goto("https://www.bilibili.com/") # self.page2.goto("https://www.bilibili.com/") self.page.goto("https://creator.xiaohongshu.com/login") self.page.fill("//input[@placeholder='手机号']","18179642779") self.page.click("//div[text()='发送验证码']") self.page.fill("//input[@placeholder='验证码']","123456") self.page.click("//span[text()=' 登 录 ']") # self.page.screenshot(path="./1.png") # 长截图 # self.page.screenshot(path="screenshot.png", full_page=True) # 截取单个元素 locator-定位器 # self.page.locator("//input[@placeholder='手机号']").screenshot(path="./1.png") # 上传文件 切换到某个iframe,然后找到上传文件元素 # self.page.frame_locator("//ifrmage1").locator("//span[@class='']").set_input_files("./1.txt") # 选择下拉框 # self.page.select_option("//*[@id='twon']",value="25609") # self.page.select_option("//*[@id='twon']",label="湖南省") # self.page.query_selector_all("") # 悬停 # self.page.hover() # 创建新的页面 with self.context.expect_page() as new_page_info: #打开新的tab页 self.page.click("a[target='_blank']") new_page = new_page_info.value new_page.wait_for_load_state() print(new_page.title()) # page.close() # page2.close() time.sleep(6) # self.browser.close() if __name__ == '__main__': xhs = XiaoHongShu() xhs.run()
如果你想在影刀中使用playwright实现多账号的切换:
第一步:
通过影刀获取已经登陆的网页上的cookie,并存保存为cookie文件,cookie.json
第二步:
1. 影刀中下载安装pip install playwright
2. 找到安装后的playwright命令 :
C:\Users\xiake\AppData\Local\ShadowBot\users\c659f209-b538-4f88-9b09-3b7411f29264\apps\802f709f-6296-4b60-8d1c-c59bf5b686cd\venv\Lib\site-packages\playwright\driver
cmd到这个目录下执行:playwright install 就可以把浏览器文件下载到本地
3.然后使用以下脚本,通过读取第一步获取到的cookie文件cookie.json,实现免登陆
import time from playwright.sync_api import sync_playwright def get_xiaohongshu_auth(cookie_file): with sync_playwright() as p: browser = p.chromium.launch(headless=False) context = browser.new_context(storage_state=cookie_file) page = context.new_page() page.goto("https://creator.xiaohongshu.com/creator/home") time.sleep(5) browser.close() if __name__ == '__main__': cookie_file="cookie.json" get_xiaohongshu_auth(cookie_file)
playwright 监听
from playwright.sync_api import sync_playwright def on_response(response, url): if 'https://edith.xiaohongshu.com/api/sns/web/v1/user_posted?' in response.url and response.status == 200: print("%s得到的响应为:" %url, response.json()) with sync_playwright() as p: browser = p.chromium.launch(headless=False) page = browser.new_page() url = 'https://www.xiaohongshu.com/user/profile/64b8dabb0000000014036a05' page.on('response', lambda response: on_response(response, url)) page.goto(url) page.wait_for_load_state('networkidle') browser.close()
有疑问可以加wx:18179641802,进行探讨