尝试将 count int 添加到字符串末尾(网址):
Code:
count = 0
while count < 20:
Url = "http://www.ihiphopmusic.com/music/page/"
Url = (Url) + (count)
#Url = Url.append(count)
print Url
I want:
http://www.ihiphopmusic.com/music/page/2
http://www.ihiphopmusic.com/music/page/3
http://www.ihiphopmusic.com/music/page/4
http://www.ihiphopmusic.com/music/page/5
Results:
Traceback (most recent call last):
File "grub.py", line 7, in <module>
Url = Url + (count)
TypeError: cannot concatenate 'str' and 'int' objects
问题正是回溯所指出的。
Python 不知道该怎么办"hello" + 12345
您必须转换整数count
首先成字符串。
此外,你永远不会增加count
变量,所以你的 while 循环将永远持续下去。
尝试这样的事情:
count = 0
url = "http://example.com/"
while count < 20:
print(url + str(count))
count += 1
或者甚至更好:
url = "http://example.com/"
for count in range(1, 21):
print(url + str(count))
正如 Just_another_dunce 指出的,在 Python 2.x 中,你还可以这样做
print url + str(count)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)