问题描述
这是我第一次编写 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
。
我希望您能提供任何帮助。谢谢!
解决方法
尝试指定 import pygame
import os
import random
import math
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Apple Catcher")
background = pygame.image.load(os.path.join("pics","farm.png"))
icon = pygame.image.load(os.path.join("pics","farmer.png"))
pygame.display.set_icon(icon)
basket_img = pygame.image.load(os.path.join("pics","basket.png"))
basket_x = 330
basket_y = 480
basket_x_change = 0
basket_y_change = 0
apple_img = pygame.image.load(os.path.join("pics","apple.png"))
apple_x = random.randint(1,768)
apple_y = random.randint(0,250)
apple_y_change = random.uniform(1,3)
score = 0
def basket(x,y):
screen.blit(basket_img,(x,y))
def apple(x,y):
screen.blit(apple_img,y))
def collision(apple_x,apple_y,basket_x,basket_y):
distance_y = math.sqrt((math.pow(apple_y - basket_y,2)))
distance_x = math.sqrt((math.pow(apple_x - basket_x,2)))
if distance_y < 1 and distance_x < 128:
return True
else:
return False
run = True
while run:
screen.fill((0,128,0))
screen.blit(background,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
basket_x_change = -3
if event.key == pygame.K_RIGHT:
basket_x_change = 3
if event.key == pygame.K_UP:
basket_y_change = -3
if event.key == pygame.K_DOWN:
basket_y_change = 3
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
basket_x_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
basket_y_change = 0
basket_x += basket_x_change
if basket_x <= 0:
basket_x = 0.0
elif basket_x >= 672:
basket_x = 672
basket_y += basket_y_change
if basket_y <= 350:
basket_y = 350
elif basket_y >= 472:
basket_y = 472
apple_y += apple_y_change
if apple_y >= 600:
apple_x = random.randint(0,768)
apple_y = random.randint(0,230)
apple_y_change = random.uniform(1,2.5)
coll = collision(apple_x,basket_y)
if coll:
apple_x = random.randint(0,2.5)
score += 1
print(score)
apple(apple_x,apple_y)
basket(basket_x,basket_y)
pygame.display.update()
HTTP 标头:
User-Agent
打印:
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)