我如何从tiktok网站获得追随者数量?

2024-09-28 17:16:25 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我第一次编写python脚本。我正试图从tiktok网站上提取一个追随者数量,并将其用作字符串。我通过进入chrome中的Inspect窗口,单击跟随者计数,然后右键单击“17M”(在本例中)来保存选择器路径,从而获得了选择器路径。昨天它运行得很好,然后突然停止了,现在无论我怎么尝试,我都无法从这个页面获得“17M”。我正在使用html请求,但一定会尝试美丽的汤或任何其他建议你

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)

我当前得到的错误是 AttributeError: 'NoneType' object has no attribute 'text' 当我运行此代码时。我希望得到17M

我希望你能提供任何帮助。谢谢大家!


Tags: text路径divurlmiddlemainsessionhtml
1条回答
网友
1楼 · 发布于 2024-09-28 17:16:25

尝试指定User-AgentHTTP头:

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)

印刷品:

17M

相关问题 更多 >