kakakakakku blog

Weekly Tech Blog: Keep on Learning!

Playwright for Python: a タグの href 属性を取得する

Playwright for Python でリンクタグ a タグの href 属性から URL を取得するときは,Locator オブジェクトで get_attribute('href') のように実装する.

page.locator('h1#title > a').get_attribute('href')
page.locator('h1.entry-title > a').nth(0).get_attribute('href')

サンプルコード

以下の例では kakakakakku blog のブログ名と最新記事の URL を取得している.

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    context = browser.new_context()

    page = context.new_page()
    page.goto('https://kakakakakku.hatenablog.com/')

    # https://kakakakakku.hatenablog.com/
    url = page.locator('h1#title > a').get_attribute('href')
    print(url)

    # https://kakakakakku.hatenablog.com/entry/2023/01/10/081119
    url = page.locator('h1.entry-title > a').nth(0).get_attribute('href')
    print(url)

    context.close()
    browser.close()