我想将玩家 id 中的每个字符串插入 url 并连接整个字符串,以便我可以抓取 url。当我在 python 中执行此操作时,出现错误
url = "https://rotogrinders.com/players/",player_id[x],"?format=json"
TypeError: list indices must be integers or slices, not str
player_id = ["stephen-curry-1079","rodney-hood-18629","c-j-mccollum-17121","damian-lillard-13900","enes-kanter-13299",
"andre-iguodala-1334","draymond-green-13976","klay-thompson-13315","andrew-bogut-1494","zach-collins-37866","kevon-looney-18945",
"al-farouq-aminu-1279","evan-turner-1291","seth-curry-16825","shaun-livingston-14007","jonas-jerebko-1106","maurice-harkless-16971",
"jordan-bell-37877","quinn-cook-31771","alfonzo-mckinnie-37808","meyers-leonard-13905","jake-layman-35216"]
x = 0
for x in player_id:
url = "https://rotogrinders.com/players/",player_id[x],"?format=json"
x+=1
基本上, the已经x
是一个字符串,但您将其视为一个整数。
你可以简单地做:
for x in player_id:
url = "https://rotogrinders.com/players/" + x + "?format=json"
或者,在 Python 3 中:
url = f"https://rotogrinders.com/players/{x}?format=json"
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句