Python - 使用函数外部变量的错误

时间:2017-09-05 05:18:32

标签: python function

我有两个Python文件: checkprice.py test2.py 。  第二个包含函数getItemInfo(),它基本上从网站获取项目的信息(特别是价格,称为item_price_edited_eur)。我需要使用 checkprice.py 文件中的价格,因此我将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()

1 个答案:

答案 0 :(得分:0)

基本上getItem()中的test2.py函数需要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()行,这样可能会让您排序。

相关问题