我正在尝试创建一个跟踪程序并使用BeautifulSoup,并要求图书馆从Steam网站上节省一些价格。然后使用openpyxl将这些价格值写入excel文件。
在前四个游戏中,一切工作都非常顺利。当我尝试将更多游戏标题添加到列表中时,该错误存在。
请参见下面的代码
将空心骑士项目添加到列表中后,我开始出现此错误:
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
完整追溯
PS C:\Python37\SteamTracker> & C:/Users/Han/AppData/Local/Programs/Python/Python37/python.exe c:/Python37/SteamTracker/main.py
c:/Python37/SteamTracker/main.py:48: DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames).
sheets = wb.get_sheet_names()
c:/Python37/SteamTracker/main.py:53: DeprecationWarning: Call to deprecated function get_sheet_by_name (Use wb[sheetname]).
sheet = wb.get_sheet_by_name(sheets[i])
successed updating Human Fall Flat
successed updating The Forest
successed updating Garry's Mod
successed updating GTA V
Traceback (most recent call last):
File "c:/Python37/SteamTracker/main.py", line 75, in <module>
updateProducts()
File "c:/Python37/SteamTracker/main.py", line 58, in updateProducts
sheet.cell(lines[i],2, value = float(price)) # updating cells
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'
答案 0 :(得分:0)
问题解决了!
好像我在这里使用的json文件在我身上造成了一个令人生畏的小错误。
当我在json中添加其他项时,我不小心将值行编码为str类型,而不是普通的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"
}
]
}
答案 1 :(得分:-1)
如错误所述,<
用于比较str
和int
。
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'
从代码行
if row < 1 or column < 1:
似乎您以str
或row
的身分通过了column
。在查看您的代码时,lines[i]
可能就是str
。
File "c:/Python37/SteamTracker/main.py", line 58, in updateProducts
sheet.cell(lines[i],2, value = float(price)) # updating cells