在函数外使用变量时发生Python错误

2024-09-30 02:21:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个Python文件:checkprice.pytest2.py。 第二个函数包含函数getItemInfo(),它基本上从网站获取商品的信息(特别是价格,称为item_price_edited_eur)。我需要使用checkprice.py文件中的price,因此我将item_price_edited_eur变量设置为getItemInfo()函数的返回值,并将变量item_price_edited_eur的值赋给函数(从这个意义上说:item_price_edited_eur = getItemInfo())。这样我就可以在函数外使用变量,但它不起作用,我得到以下错误:

Traceback (most recent call last): File "checkprice.py", line 25, in <module> item_price_edited_eur = getItemInfo() TypeError: getItemInfo() missing 5 required positional arguments: 'item_url', 'item_name_xpath', 'item_game_xpath', 'item_price_xpath', and 'item_quantity_sold_xpath'

我怎样才能解决这个问题


这是test2.py文件:

from selenium import webdriver
#from selenium.webdriver.common.keys import Keys

def getItemInfo(item_url,item_name_xpath,item_game_xpath,item_price_xpath,item_quantity_sold_xpath):
    #info includes item name, game, quantity sold and price
    from forex_python.converter import CurrencyRates
    from selenium import webdriver
    path_to_chromedriver = 'C:\\Users\\Emanuele-PC\\Desktop\\chromedriver\\chromedriver.exe'
    browser = webdriver.Chrome(executable_path = path_to_chromedriver)
    browser.get(item_url)
    item_name = browser.find_element_by_xpath(item_name_xpath).text
    item_game = browser.find_element_by_xpath(item_game_xpath).text
    item_price = browser.find_element_by_xpath(item_price_xpath).text
    item_quantity_sold = browser.find_element_by_xpath(item_quantity_sold_xpath).text
    #item_price isn't an integer, it's a string, 'Prezzo iniziale: $' and 'USD' need to be cut with .replace, then converted to int with int()
    #item_price_edited is the int() variable, ready to be used
    item_price_cut1 = item_price.replace('Prezzo iniziale:','')
    item_price_cut2 = item_price_cut1.replace('$','')
    item_price_cut3 = item_price_cut2.replace('USD','')
    item_price_edited_usd = float(item_price_cut3)
    #the value of the price is USD, it needs to be converted to EUR
    c = CurrencyRates()
    #round arrotonda il valore con la virgola a due numeri decimali
    item_price_edited_eur = round(c.convert('USD', 'EUR', item_price_edited_usd),2)
    #print(item_name)
    #print(item_game)
    #print(item_price_edited_eur)
    #print(item_quantity_sold)
    return item_price_edited_eur

这是checkprice.py文件:

import mysql.connector
from test2 import getItemInfo
from time import gmtime, strftime

#getItemInfo requires as inputs(item_url,item_name_xpath,item_price_xpath,item_quantity_sold_xpath)

conn = mysql.connector.connect(host='localhost',user='user1',password='pass1',db='mm')
cursor = conn.cursor()
select_query = """SELECT * FROM items_basic_info"""
cursor.execute(select_query)
rows = cursor.fetchall()
for row in rows:
    #row[0] is id
    #row[1] is item_name
    #row[2] is item_name_xpath
    #row[3] is item_game
    #row[4] is item_game_xpath
    #row[5] is item_price_xpath
    #row[6] is item_url
    #row[7] is item_quantity_sold_xpath
    getItemInfo(row[6],row[2],row[4],row[5],row[7])
    date_time = strftime("%Y-%m-%d %H:%M:%S", gmtime())
    #insert_query inserts price of items in items_price
    update_query = """UPDATE items_price SET item_price = %s, last_updated = %s WHERE item_name = %s"""
    item_price_edited_eur = getItemInfo()   
    cursor.execute(update_query,(item_price_edited_eur, date_time, row[1]))
    conn.commit()

Tags: tonamepyimportgameiseuritem
1条回答
网友
1楼 · 发布于 2024-09-30 02:21:40

基本上,test2.py中的getItem()函数需要5个参数

def getItemInfo(item_url,item_name_xpath,item_game_xpath,item_price_xpath,item_quantity_sold_xpath):

您不是在调用形式checkprice.py中提供它们,您是在没有参数的情况下调用它的

item_price_edited_eur = getItemInfo()

这就是python抛出的错误,在checkprice.py中的调用中,需要传递所有参数

编辑: 在这一行item_price_edited_eur = getItemInfo()您再次调用函数

您需要做的是将这一行getItemInfo(row[6],row[2],row[4],row[5],row[7])更改为item_price_edited_eur = getItemInfo(row[6],row[2],row[4],row[5],row[7]),并删除带有item_price_edited_eur = getItemInfo()的行,这样您就有希望排序了

相关问题 更多 >

    热门问题