David
David

Reputation: 23

How can I get a follower count from the tiktok website?

This is my first time ever writing a python script. I'm trying to pull a follower count from the tiktok website and use it as a string. I got the selector path from going into the Inspect window in chrome, clicking on the follower count, and right clicking "17M" (in this example) to save the selector path. This worked perfectly yesterday, and then all of the sudden it stopped and now I can't get the "17M" from this page no matter what I try. I'm using requests-html but would definitely try beautiful soup or whatever other recommendation you have!

from requests_html import HTMLSession
session = HTMLSession()
url = "https://www.tiktok.com/@terrycrews"
r = session.get(url)
sel = '#main > div.jsx-2773227880.main-body.page-with-header.middle.em-follow > div.jsx- 2938571814.share-layout.compact.middle > div > header > h2.count-infos > div:nth-child(2) > strong'
followers = r.html.find(sel, first=True).text
print(followers)

The current error I get is AttributeError: 'NoneType' object has no attribute 'text' when I run this code. I'd expect to get 17M.

I would love any help you might be able to provide. Thank you!

Upvotes: 0

Views: 5698

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195438

Try to specify User-Agent HTTP header:

import requests
from bs4 import BeautifulSoup

url = "https://www.tiktok.com/@terrycrews"

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0"
}

soup = BeautifulSoup(requests.get(url, headers=headers).content, "html.parser")
print(soup.select_one('[title="Followers"]').text)

Prints:

17M

Upvotes: 3

Related Questions