Python 获取类型错误:应为字符串或类似字节的对象
我的代码:Python 获取类型错误:应为字符串或类似字节的对象,python,Python,我的代码: import urllib.request for num in range(1,31): URL= "http://readcomicbooksonline.net/reader/mangas/Batman 2011/Batman Endgame Special Edition/njqr-" IMAGE = str(num)+".jpg" for i in range(1,(3-int(num/10))): IMAGE="0"+IMAGE
import urllib.request
for num in range(1,31):
URL= "http://readcomicbooksonline.net/reader/mangas/Batman 2011/Batman Endgame Special Edition/njqr-"
IMAGE = str(num)+".jpg"
for i in range(1,(3-int(num/10))):
IMAGE="0"+IMAGE
print (IMAGE)
req = urllib.request.Request(str(URL+IMAGE), headers={'User-Agent': 'Mozilla/5.0'})
urllib.request.urlretrieve(req, str(IMAGE))
错误:
Traceback (most recent call last):
File "E:\Books, Papers and Certificates\Comics\Batman\EndGame\downloader.py", line 9, in <module>
urllib.request.urlretrieve(req, str(IMAGE))
File "C:\Users\Baidyanath Kundu\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 246, in urlretrieve
url_type, path = splittype(url)
File "C:\Users\Baidyanath Kundu\AppData\Local\Programs\Python\Python36-32\lib\urllib\parse.py", line 924, in splittype
match = _typeprog.match(url)
TypeError: expected string or bytes-like object
回溯(最近一次呼叫最后一次):
文件“E:\Books,Papers and Certificates\Comics\Batman\EndGame\downloader.py”,第9行,在
urllib.request.urlretrieve(req,str(IMAGE))
文件“C:\Users\Baidyanath Kundu\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py”,第246行,在urlretrieve中
url\u类型,路径=拆分类型(url)
文件“C:\Users\Baidyanath Kundu\AppData\Local\Programs\Python\Python36-32\lib\urllib\parse.py”,第924行,拆分类型
匹配=_typeprog.match(url)
TypeError:应为字符串或类似字节的对象
编辑对不起,我的错我发布了以前版本的代码。我已经更新了它。您能描述一下您正在尝试实现什么,以及到目前为止您已经尝试了什么吗?堆栈跟踪显示错误在
urllib.request.urlretrieve(req,str(IMAGE))
行中,但该行不在您的示例中。我运行了你的示例,得到了一个HTTP“禁止”错误,但是url解析的东西起作用了。问题在于你没有显示的代码。我正在尝试从URL下载所有图像。