When I used with open to download pictures, I encountered permission denied.
When I reviewed the code, when I was on Baidu, I didn't find out what happened, asked a friend, and finally solved it.
#这里是部分代码,然而,问题也就出现在这
tupian = strhtml_xpath.xpath("//center//img/@src")
for tu in tupian:
with open(r'E:/图片','ab') as fp:
print("正在下载...")
fp.write(tu.content)
As shown in the figure, the tupian found, its value is a picture link, is a list (list), you want to continue to use requests to continue the request, use write ().
Just like:
tupian = strhtml_xpath.xpath("//center//img/@src")
for tu in tupian:
#请求tu的链接
t = requests.get(tu)
#给图片起名
titles = tu.split('/')[-1]
#在下载图片时,要给图片起名,否则下载不了。
with open(r"E:/图片/{}".format(titles),'ab') as fp:
print("正在下载...{}".format(titles))
fp.write(t.content)
to sum up:
When using with open to download pictures:
- After getting the list of URLs of the pictures, you must request the request before writing using the write () method.
- When downloading pictures, give them a name.