首页 > 解决方案 > TypeError:“str”对象不可调用以从网站抓取数据

问题描述

我想每秒在网站上打印一些东西的价格,但我收到错误“TypeError:'str' object is not callable”

这是我的代码:

import requests
from bs4 import BeautifulSoup
import time

url = "https://www.roblox.com/catalog/20573078/Shaggy"
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
newprice = soup.find("span", {"class": "text-robux-lg wait-for-i18n-format-render"}).text
a = 1

while a == 1:
    time.sleep(1)
    print(newprice())

标签: python

解决方案


您正在打印一个函数,而不是变量。只需使用:

print(newprice)

输出:

在此处输入图像描述

编辑

您正在抓取的价格不会更新,因为您已经将信息放入该变量中。为了实现您想要的,您还需要在循环中进行抓取,如下所示:

url = "https://www.roblox.com/catalog/20573078/Shaggy"
a = 1

while a == 1:
    soup = BeautifulSoup(requests.get(url).content, 'html.parser')
    newprice = soup.find("span", {"class": "text-robux-lg wait-for-i18n-format-render"}).text
    time.sleep(1)
    print(newprice)

这将使得每次运行 while 循环时,它都会从网站获取数据。


推荐阅读