首页 > 解决方案 > 为什么我会收到“语法警告对象不可调用;可能缺少逗号”?

问题描述

我正在尝试编写一个程序来监控网站上的鞋子价格,但我收到一条错误消息:

syntaxwarning object not callable;perhaps you missed a comma?

这是我的程序的代码:

from selenium import webdriver
import time

class snkrsBot:

def __init__(self, sneaker_url):
    self.sneaker_url = sneaker_url
    self.driver = webdriver.Chrome('./chromedriver.exe')

def get_price(self):    
    self.driver.get(self.sneaker_url)
    price = self.driver.find_element_by_xpath('//div[@data-test="product-price"]')
    return int(price.get_attribute('innerHTML').strip('£'))

def main():
    url = 'https://www.nike.com/gb/t/air-max-95-essential-shoe-7hwG30/AT9865-001'
    bot = snkrsBot(url) 
 last_price = None
    while True:
        price = bot.get_price()
        if last_price:
            if price < last_price:
                print('Price dropped:'(last_price - price))
            elif price > last_price:
                print('Price rose:'(price - last_price))
            else:
                print('Price is the same'(price))    
        last_price = price
        time.sleep(5)

标签: pythonwindowsselenium

解决方案


print的都是错的;错误告诉你你试图调用一个str文字,就好像它是一个带参数的函数一样。改成:

        if price < last_price:
            print('Price dropped:', last_price - price)
        elif price > last_price:
            print('Price rose:', price - last_price)
        else:
            print('Price is the same', price) 

我所做的只是添加三个逗号,并删除要打印的值周围的无意义的括号。


推荐阅读