playwright 操作

import time
import csv
from playwright.sync_api import Playwright, sync_playwright


with sync_playwright() as playwright:
    browser = playwright.chromium.launch(headless=False)
    # 打开一个浏览器会话
    context = browser.new_context()
    context.clear_cookies()
    # 打开一个新页面
    page = context.new_page()
    page.goto("https://www.dangdang.com/")
    input_ = page.query_selector('#key_S')
    input_.fill('管理学')
    time.sleep(0.4)
    page.query_selector('.button').click()
    time.sleep(4)
    print('dd')
    for i in range(100):
        ul = page.query_selector('#component_59')
        li_list = ul.query_selector_all('li')
        for li in li_list:
            url_ = 'https:' + li.query_selector('.name a').get_attribute('href')
            title = li.query_selector('.name a').get_attribute('title')

            with open('1.csv', 'a+', encoding='utf-8', newline='') as f:
                wf = csv.writer(f)
                wf.writerow([url_, title])
            pass
        page.query_selector('.next a').click()
        time.sleep(5)

posted @ 2024-04-01 18:16  hziwei  阅读(21)  评论(0编辑  收藏  举报