每次机器人运行时如何更新函数的特定参数?

问题描述 投票:0回答:2

我正在使用gspread和IMDbPy编写机器人。脚本现在需要输入(电影标题),然后获取电影ID,在IMDB.com上找到电影的分级,然后将分级发布到电子表格的特定单元格中。

有一个名为“ update_cell”的函数,该函数根据给定的行和列参数更新特定的单元格。一旦机器人完成,我就不必继续研究代码来更新行单元格参数。我希望每次机器人执行时将其更新1。

有没有办法做到这一点?我将在下面发布代码:

ia = imdb.IMDb()


def take_input():
    fd = open('movielist.txt',"w")
    print("Input your movie please: \n")
    inp = input()
    fd.write(inp)
    fd.close()

take_input()

# Wed 8/28/19 - movie_list is a list object. Must set it equal to our ia.search_movies
# Need to find out where to put movie_list = ia.search_movies in the code, and what to 
# remove or keep.


a = int(52)
b = int(18)



def Main():
    c = """Python Movie Rating Scraper by Nickydimebags"""
    print(c)
    time.sleep(2)
    f1 = open('movielist.txt')
    movie_list = []

    for i in f1.readlines():
        movie_list.append(i)
        movie_list = ia.search_movie(i)
        movie_id = movie_list[0].movieID
        print(movie_id)
        m = ia.get_movie(movie_id)
        print(m)
        rating = m['rating']
        print(rating)
        scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',                    "https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
        creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
        client = gspread.authorize(creds)
        sheet = client.open("Movie Fridays").sheet1
        sheet.update_cell(a, b, rating) #updates specific cell

Main()

^ a变量是我每次机器人运行时都需要更新1的变量

python iteration bots gspread imdbpy
2个回答
1
投票

我猜a变量跟踪行索引。您可以在将值添加到的列中获得下一个空行单元格的索引。

def next_available_row(worksheet, col):
    return len(worksheet.col_values(col)) + 1


sheet = client.open("Movie Fridays").sheet1
sheet.update_cell(next_available_row(sheet, b), b, rating)

0
投票

您将需要将变量的当前值或下一个值保存在某个位置,并在每次脚本运行时对其进行更新。

您可以为此滥用电子表格中的单元格,或将其写到文件中。

© www.soinside.com 2019 - 2024. All rights reserved.