Search code examples
pythondiscord.pypyppeteer

Trying to websrap with python, pypeteer


Goal is to pull the information off of a website that tracks tiktok followers and post it in console/send in discord channel. Currently using discord to initiate it but having it print in console. Current code listed below prints:

[<pyppeteer.element_handle.ElementHandle object at 0x00000214B2703640>]

@bot.command()
async def stats(ctx):
    statspage = await browser.newPage()
    await statspage.goto('https://livecounts.io/tiktok-live-follower-counter/charlieputh')
    t = await statspage.xpath('//*[@id="__next"]/div/div/div[3]/div[2]/div/div/div/div')
    print(t)

I would like it to return with the amount of followers listed on that page. Please help.


Solution

  • The page.xpath function gives you elements' list, not text. If you want to get text of element you need to evaluate it, like:

    elements = await statspage.xpath('//*[@id="__next"]/div/div/div[3]/div[2]/div/div/div/div')
    text = await page.evaluate("e => e.innerText", elements[0])
    

    As you might know, pyppeteer is an unofficial Python version of puppeteer, so you should see documentation of puppeteer to see how it works. And also docs of pyppeteer to see what are differences in Python version.