我在使用with open下载图片时,遇到了 权限被拒绝。
当我审查代码时,当我在网上百度时,也没查出个究竟,问了一位好友,终于把它解决了。
#这里是部分代码,然而,问题也就出现在这
tupian = strhtml_xpath.xpath("//center//img/@src")
for tu in tupian:
with open(r'E:/图片','ab') as fp:
print("正在下载...")
fp.write(tu.content)
如图,找到的tupian,它的值是一个个图片链接,是一个列表(list),你要接着用requests继续请求后,在使用write()。
正如:
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)
总结:
使用with open 下载图片时:
- 得到了图片的url的列表,要先requests请求,然后才能使用write()方法写入。
- 在下载图片时,要给图片起名。