# coding = utf-8"""爬取百度壁纸图片
"""/*
优秀开源电商系统学习地址:http://github.crmeb.net/u/fei
*/import requests,re,time,warnings,os
warnings.filterwarnings("ignore")def search(key_word):# 百度获取风景壁纸图片Url地址search_url = "https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1579231773363_R&pv=&ic=0&nc=1&z=0&hd=0&latest=0©right=0&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&sid=&word=" + key_word + ""response = requests.get(url=search_url, verify=False)return response.textdef find_picture_url(response):# 正则查找字符串结果中的图片Url地址返回列表regex = re.compile(r'"objURL":"(.*?)"')picture_urls = regex.findall(response)return picture_urls# print(picture_urls)def down_load_picture(picture_urls):# 图片下载文件夹地址file_ = "D:\\python脚本\\picture\\"# 判断文件夹路径是否存在不存在创建一个if not os.path.exists(file_):os.makedirs(file_)number = 1for picture_url in picture_urls:time.sleep(0.5)picture_name = file_ + str(int(time.time())) + ".jpg"print("\r", "正在下载第{0}张图片:{1}".format(str(number), picture_name), end="")try:r = requests.get(picture_url,verify=False,timeout=10)# 图片要二进制写入with open(picture_name, "wb") as f:f.write(r.content)number += 1except:print("\r", "下载失败:{0}".format(picture_name), end="")continue# 主程序
print("【下载图片程序】说明:输入 exit 退出程序\n")
while True:input_word = input("\n请输入所要下载图片的关键字,如:风景壁纸...")print("\n")if input_word == "exit":print("\n关闭程序...")breakelse:search_result = search(key_word=input_word)picture_urls = find_picture_url(response=search_result)down_load_picture(picture_urls=picture_urls)
print("下载完毕!")