我不知道为什么我会得到“TypeError:'<'在'str'和'int'实例之间不受支持”

2024-05-23 22:36:10 发布

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

我正在尝试创建一个跟踪器程序,并使用BeautifulSoup和requests库来降低Steam网站的价格。然后使用openpyxl将这些价格值写入excel文件。在

在前四场比赛中,一切都完美无瑕。当游戏中存在更多的标题时,我尝试添加更多的错误。在

参见下面的代码

在我将空心骑士物品添加到wistlist数组后,我开始得到以下错误:

TypeError:“str”和“int”的实例之间不支持“<;”

即使我没有用任何比较符号来比较这个程序中的值。在

旁注: 我有一个json文件,它存储了将值放入excel的下一行。程序在执行完上面的所有代码后,会将行值更新1。请参见下面的函数updateTrackingLines()

cookies = {'birthtime': '568022401'} # For Steam's age verification

wishlist = [
    "https://store.steampowered.com/app/477160/Human_Fall_Flat/",
    "https://store.steampowered.com/app/242760/The_Forest/",
    "https://store.steampowered.com/app/4000/Garrys_Mod/",
    "https://store.steampowered.com/app/271590/Grand_Theft_Auto_V/",
    "https://store.steampowered.com/app/367520/Hollow_Knight/",
    "https://store.steampowered.com/app/588650/Dead_Cells/s",
    "https://store.steampowered.com/app/320240/We_Happy_Few/", "https://store.steampowered.com/app/589510/Shovel_Knight_Specter_of_Torment/",
    "https://store.steampowered.com/app/413150/Stardew_Valley/"
]
l = len(wishlist)
def updateProducts():
    wb = openpyxl.load_workbook("C:\Python37\SteamTracker\ProductsTracker.xlsx")
    sheets = wb.get_sheet_names()
    today = date.today()
    today = today.strftime("%m/%d/%Y")
    lines = getTrackingLines()
    for i in range(0,l):
        sheet = wb.get_sheet_by_name(sheets[i])
        html = requests.get(wishlist[i],cookies=cookies)
        page_soup = soup(html.content, "html.parser")
        price = page_soup.find("div",{"class":"game_purchase_price price"}).text.strip()
        price = price[1:len(price)]
        sheet.cell(lines[i],2, value = float(price)) # updating cells
        sheet.cell(lines[i],1, value = today)
        print("successed updating " + sheet.title)
    wb.save("C:\Python37\SteamTracker\ProductsTracker.xlsx")
    updateTrackingLines() # See explaination of this func above

完全回溯

^{pr2}$

Tags: storehttps程序comappgettodayhtml
2条回答

问题解决了!在

看起来我用json文件在这里犯了一个小错误。在

当我在json中添加额外的项时,我意外地将值行编码为str-type而不是纯int

我们需要一个int值来定位excel表中的单元格,但是我的代码值行是str类型的。在

我最好学会小心打字。在

感谢您的帮助和参与这个主题

{
  "titles": [
    {
      "name": "Human Fall Flat",
      "line": 6
    },
    {
      "name": "The Forest",
      "line": 6
    },
    {
      "name": "Garry's Mod",
      "line": 6
    },
    {
      "name": "Grand Theft Auto V",
      "line": 6
    },
    {
      "name": "Hollow Knight",
      "line": "2"
    },
    {
      "name": "Dead Cells",
      "line": "2"
    },
    {
      "name": "We Happy Few",
      "line": "2"
    },
    {
      "name": "Shovel Knight: Specter of Torment",
      "line": "2"
    },
    {
      "name": "Stardew Valley",
      "line": "2"
    }
  ]
}

如错误所述,<用于比较str和{}。在

  File "C:\Users\Han\AppData\Local\Programs\Python\Python37\lib\site-packages\openpyxl\worksheet\worksheet.py", line 236, in cell
    if row < 1 or column < 1:
TypeError: '<' not supported between instances of 'str' and 'int'

从代码行开始

^{pr2}$

似乎您传递了strrow或{}。在查看代码时,lines[i]可能就是str。在

  File "c:/Python37/SteamTracker/main.py", line 58, in updateProducts
    sheet.cell(lines[i],2, value = float(price)) # updating cells

相关问题 更多 >