from playwright.sync_api import sync_playwright import os class Tool: def __init__(self, user_data_dir, executable_path): playwright = sync_playwright().start() # 启动扩展程序-开启影刀插件 path_to_extension = r"D:\data\google\Chrome3\Default\Extensions\nhkjnlcggomjhckdeamipedlomphkepc\1.1_0" # 启动本地chrome浏览器,并加载插件 self.context = playwright.chromium.launch_persistent_context(headless=False, args=["--start-maxmized", f"--disable-extensions-except={path_to_extension}", f"--load-extension={path_to_extension}", '--disable-blink-features=AutomationControlled'], slow_mo=3000, user_data_dir=user_data_dir, executable_path=executable_path ) self.context.expect_page() def on_response(self, response, url): print(response.url) if "https://h5api.m.tmall.com/h5/mtop.taobao.pcdetail.data.get" in response.url and response.status == 200: # print(json.dumps(response.json(),ensure_ascii=False)) html = response.text() self.parse(html) def parse(self): pass def spider_detail(self, url): page = self.context.new_page() # 开启网页监听 page.on("response", lambda response: self.on_response(response, url)) page.goto(url) page.wait_for_timeout(1000) html = page.content() print(html) page.close() def run(self): url = "https://detail.tmall.com/item.htm?id=740112373558&pvid=3fca9b71-80a0-45bb-9a4c-e1446649e7b8&scm=1007.40986.276750.0&spm=a21bo.jianhua.201876.22.5af92a89AECsv3&sku_properties=1627207:11167238" self.spider_detail(url) self.context.close() if __name__ == '__main__': user_data_dir = 'D:\data\google\Chrome1' executable_path = r"C:\Users\19706\AppData\Local\Google\Chrome\Application\chrome.exe" tool = Tool(user_data_dir, executable_path) tool.run()
1.以上代码开启本地浏览器,并开启插件
2. 网页监听
标签:插件,playwright,启动,url,self,page,path,data,response From: https://www.cnblogs.com/knighterrant/p/17790424.html