利用playwright控制B站搜索进行下一个操作,知道下一页没有为止

import asyncio
from playwright.async_api import async_playwright
from playwright.sync_api import sync_playwright

# B站同步翻页功能
def bilibili_page_turning_sync():
    with sync_playwright() as p:
        # 启动浏览器
        try:
            browser = p.chromium.launch(headless=False)
            context = browser.new_context()
            page = context.new_page()
            page.goto("https://www.bilibili.com/")
            page.get_by_role("textbox").fill("手工耿")
            with page.expect_popup() as page1_info:
                page.get_by_role("textbox").press("Enter")
            page1 = page1_info.value
            next_page = page1.get_by_role("button", name="下一页")      # 找到下一个元素<button class="vui_button vui_pagenation--btn vui_pagenation--btn-side">下一页</button>, 当到最后一页时候,button会有disabled属性为空,则退出循环
            while next_page.get_attribute("disabled") is None:      
                next_page.click()
                next_page = page1.get_by_role("button", name="下一页")
                element = page1.locator(".vui_button--active-blue")     # 当前页面的元素
                if element:
                    page_no = element.text_content()
                    print(f"当前页数: {page_no}")
                else:
                    print("未找到元素")
                page1.wait_for_timeout(3000)        # 停留3秒
            print("翻页完毕")

        finally:
            # --------------------- 资源释放
            context.close()
            browser.close()

async def bilibili_page_turning_async():
    async with async_playwright() as p:
        # 启动浏览器
        try:
            browser = await p.chromium.launch(headless=False)
            context = await browser.new_context()
            page = await context.new_page()
            await page.goto("https://www.bilibili.com/ ")
            await page.get_by_role("textbox").fill("手工耿")
            async with page.expect_popup() as page1_info:
                await page.get_by_role("textbox").press("Enter")
            page1 = await page1_info.value
            # 找到下一个元素<button class="vui_button vui_pagenation--btn vui_pagenation--btn-side">下一页</button>, 当到最后一页时候,button会有disabled属性为空,则退出循环
            next_page = page1.get_by_role("button", name="下一页")              # 为什么这里不用await 
            next_page_flag = await next_page.get_attribute("disabled") 
            while next_page_flag is None:
                await next_page.click()
                next_page = page1.get_by_role("button", name="下一页")          # 为什么这里不用await 
                element =  page1.locator(".vui_button--active-blue")            # 当前页面的元素
                if element:
                    page_no = await element.text_content()
                    print(f"当前页数: {page_no}")
                else:
                    print("未找到元素")
                await page1.wait_for_timeout(3000)                              # 停留3秒
            print("翻页完毕")

        finally:
            # --------------------- 资源释放
            await context.close()
            await browser.close()

# b站翻页操作
if __name__ == '__main__':
    # 运行脚本
    # bilibili_page_turning_sync()
    asyncio.run(bilibili_page_turning_async())

b站页面悬停、然后点击操作

import re
from playwright.sync_api import sync_playwright


# 悬浮窗口
def test_hover():
    with sync_playwright() as p:
        try:
            browser = p.chromium.launch(headless=False)
            page = browser.new_page()
            page.goto("https://www.bilibili.com/")

            # 根据文本内容选择所有元素   包含11的文本
            lcs = page.get_by_role("link", name="番剧").nth(1)
            lcs.hover()
            dropdown_item = page.get_by_role("link", name="番剧索引")
            dropdown_item.click()
            page.wait_for_timeout(2000)
            print(lcs)
        finally:
            # 关闭浏览器
            browser.close()


# get_by_text()
if __name__ == '__main__':
    test_hover()
Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐