点此免费加入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)

点此免费加入Python网络爬虫学习交流QQ群:428518750

picture loss