python - 如何从 tiktok 网站获取关注者数量?
问题描述
这是我第一次编写 python 脚本。我正在尝试从 tiktok 网站中提取关注者数量并将其用作字符串。我从进入 chrome 中的 Inspect 窗口得到选择器路径,单击关注者计数,然后右键单击“17M”(在此示例中)以保存选择器路径。这昨天工作得很好,然后突然停止了,现在无论我尝试什么,我都无法从这个页面获得“17M”。我正在使用 requests-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
.
我希望您能提供任何帮助。谢谢!
解决方案
尝试指定User-Agent
HTTP 标头:
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
推荐阅读
- javascript - Angular:“解决”生成一个无限循环
- reactjs - React.js:根据 API 响应自定义侧边栏菜单
- python-3.x - 当我使用字典调用它时,函数打印无
- python-3.x - 如何在 tensorflow 2.0.0 中使用 Lazy Adam 优化器
- firebase - 有没有一种方法可以从 Firestore 的单独集合中的集合中查找数组的值?
- c# - ASP.NET WebMethod 接收 JSON 字符串?
- javascript - javascript while循环可以在10毫秒内执行一次
- kentico - 找不到支持服务 CMS.Helpers.ICurrentCookieLevelProvider 的组件
- azure-devops - 我们可以在 devops 中创建逻辑应用程序时参数化 HTTP POST 请求的 url
- laravel - stancl/tenancy 自定义数据库名称