python - 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())
解决方案
您正在打印一个函数,而不是变量。只需使用:
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 循环时,它都会从网站获取数据。
推荐阅读
- angular - 如何在 Angular 6 中使用 ag-grid 的导出到 excel 功能时将 excel 中的列的日期格式设置为 (dd/mm/yyyy)
- powershell - Powershell 查找和移动文件
- python-3.x - Chromedriver另存为PDF方法不起作用
- java - 如何解决 Android Studio 在构建/编译中读取 XML 文件的错误?
- android - Android:Espresso:按意图初始化活动 - java.lang.RuntimeException:无法启动活动
- mysql - 如何使用python从数据库中检索图像
- xslt - XSL IF 两个变量似乎永远不会比较
- sql - 如何将变量值添加到 Codeigniter 中的选择查询结果?
- google-bigquery - 将 Google Cloud SQL 导出为 CSV 时的自定义分隔符
- android - 如何在 dagger2 MVVM 中创建进度/错误视图?