python - TypeError:只能连接str
问题描述
我正在制作一个加菲猫视频下载器。我遇到了这个错误,不知道如何解决。
Error:
Traceback (most recent call last):
File "/Users/Jake/Desktop/garfield copy 2.py", line 10, in <module>
url = 'http://pt.jikos.cz/garfield/' + year + '/' + month
TypeError: can only concatenate str (not "int") to str
代码:
# Downloads Garfield Comic
import requests, os, bs4, threading
print('\t\t---------Garfield Comic Download---------')
t = 1
print('Enter the Year:')
year = input()
month = 1
while t < 12:
url = 'http://pt.jikos.cz/garfield/' + year + '/' + month
location = 'Garfield(' + month + '-' + year + ')'
os.makedirs(location, exist_ok=True)
def downloadGarfield(comicElem, startNum, endNum):
# Download the images
for i in range(startNum, endNum):
comicUrl = comicElem[i].get('src')
# Download the image.
print('Downloading the image %s...' % (comicUrl))
res = requests.get(comicUrl)
res.raise_for_status()
# Save the image to ./Garfield(month-year).
imageFile = open(os.path.join(location, os.path.basename(comicUrl)), 'wb')
for chunk in res.iter_content(100000):
imageFile.write(chunk)
imageFile.close()
# Download the page.
print('Downloading page %s...' % url)
res = requests.get(url)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text,'lxml')
# Find the URL of the comic image.
comicElem = soup.select('table img')
downloadThreads = []
for i in range(0, len(comicElem), 2):
downloadThread = threading.Thread(target = downloadGarfield, args=(comicElem, i, min(len(comicElem), i+2)))
downloadThreads.append(downloadThread)
downloadThread.start()
# Wait for all threads to end.
for downloadThread in downloadThreads:
downloadThread.join()
month + 1
t + 1
print('Done')
解决方案
您不能将整数连接到字符串,您需要先将其转换为字符串。
str(month)
推荐阅读
- sql-server - 创建在本地目录中生成 csv 文件的存储过程
- powershell - 如何将模拟的 PSCustomObject 作为 SQLConnection 参数传递
- bash - 我是否应该在 bash 脚本中转义变量
- clojure - 揭开 Clojure / ClojureScript 入门的神秘面纱
- excel - 将宏分配给列表框 OnSelectionChange(不是 OnAction)
- ios - 如何在 swift 中的 Uibutton 中运行异步函数
- javascript - 与sreenwidth相关的左侧或右侧的动态导航菜单boostrap
- angular - 如何在角度选择文件之前和之后启用/禁用按钮
- typescript - 使用 Preact + Typescript 的类型安全事件处理程序
- cordova - Cordova-pdf-generator:无法使用相对路径绑定图像