playwright 操作

hziwei發表於2024-04-01
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)

相關文章