Python爬虫入门之爬取图片
import requests
import os
url = input("Please input URL\n")
root = "D://pics//"
kv = {
"User-agent":"Mozilla/5.0"
}
path = root + url.split(\'/\')[-1] #本地存储目录
try:
if not os.path.exists(root):
os.mkdir(root)
if not os.path.exists(path):
r = requests.get(url,headers = kv)
print(r.request.headers)
with open(path,"wb") as f:
f.write(r.content)
f.close()
print("Win!!!")
else :
print("Oh,no!!!")
except:
print("Oh,shit!!!")
版权声明:本文为vocaloid01原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。