Python:将url与一个整数连接起来

Python: Concatenate url with a integer

我是 python 的新手,我正在尝试将 url 与一个整数连接起来,但出现此错误:

  • TypeError: cannot concatenate 'str' and 'int' objects*

我的代码如下所示:

for x in range(0,10):
   url = "http:www.eluniversal.com.mx/minuto-x-minuto?seccion=All&page="+(x)
   x +=1

有人可以帮助我吗?

Python 是一种动态的 strongly typed 语言。因此,当您尝试连接它们时,它不会将整数转换为字符串。

您必须使用字符串插值或将其显式转换为字符串。

url = "http:www.eluniversal.com.mx/minuto-x-minuto?seccion=All&page=%s" % x

url = "http:www.eluniversal.com.mx/minuto-x-minuto?seccion=All&page=" + str(x)
url = "http:www.eluniversal.com.mx/minuto-x-minuto?seccion=All&page="+str(x)

在你的括号前加一个"str"就可以了。通过这样做,您可以将 int 转换为字符串。

url += str(x)

x+=1没有效果。