通過pyppeteer來爬取今日頭條

import asyncio
from pyppeteer import launch


async def main():
    browser = await launch()
    page = await browser.newPage()
    await page.setViewport(viewport={'width': 1280, 'height': 800})

    # 是否啓用js
    await page.setJavaScriptEnabled(enabled=True)

    await page.goto('https://www.toutiao.com')

    # 打印cookie頁面
    print(await page.cookies())

    # await asyncio.sleep(5)
    await asyncio.sleep(2)
    title_ele = await page.xpath('//div[@class="title-box"]/a')

    for item in title_ele:
        # title_str = await (await item.getProperty('textContent')).jsonValue()
        print(await (await item.getProperty('textContent')).jsonValue())
        title_link = await (await item.getProperty('href')).jsonValue()
        print(title_link)
        # print(await item.jsonValue())

    await browser.close()

    # # 打印頁面文本
    # print(await page.content())
    #
    # # 打印當前首頁的標題
    # print(await page.title())

asyncio.run(main())

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章