如何将一个字符串插入另一个?

时间:2019-05-14 18:18:08

标签: python web-scraping string-concatenation

我想将播放器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

1 个答案:

答案 0 :(得分:4)

基本上,x已经是一个 string ,但是您将其视为整数。

您可以简单地做:

for x in player_id:
    url = "https://rotogrinders.com/players/" + x + "?format=json"

或者,在Python 3中:

url = f"https://rotogrinders.com/players/{x}?format=json"