点此免费加入Python网络爬虫学习交流QQ群:428518750
通过设置reponse的回调函数,可以获取接口的请求参数及响应数据。
from playwright.sync_api import Playwright, sync_playwright import json def handle_json(json): for p in json['list']: id = p['id'] prodName = p['prodName'] prodCat = p['prodCat'] avgPrice = p['avgPrice'] place = p['place'] pubDate = p['pubDate'] print(id, prodName, prodCat, avgPrice, place, pubDate) def handle(response): if response is not None: if response.url == 'http://www.xinfadi.com.cn/getCat.html': print(response.request.url) print(response.request.post_data) handle_json(response.json()) def run(playwright: Playwright) -> None: browser = playwright.chromium.launch(headless=False) context = browser.new_context(ignore_https_errors=True) page = context.new_page() page.on("response", lambda response: handle(response=response)) url = 'http://www.xinfadi.com.cn/index.html' page.goto(url) page.wait_for_timeout(2000) context.close() page.close() browser.close() with sync_playwright() as playwright: run(playwright)
本站所有内容均为原创,本站保留所有权利。仅允许非商业用途的转载,但必须注明来源网站、作者、来源链接!否则,由此造成的一切后果,由转载方承担!