import requests
import os
import re
# 新建一个文件夹
# 电脑问我们你想下载什么图片
wenti = input("你想下载什么图片?")
try:
os.mkdir(wenti)
print('%s文件夹已经为您新建好'%wenti)
except:
print('%s文件夹已经为您新建好'%wenti)
# 获取源代码
url = r'http://image.baidu.com/search/flip?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1497491098685_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&ctd=1497491098685%5E00_1519X735&word='
url = url+wenti
print(url)
huoqu = requests.get(url)
# 用正则表达式提取里面每一个图片的链接地址
plianjie = re.findall('"objURL":"(.*?)",',huoqu.text)
# 批量下载
print(plianjie)
for i in range(10):
print(plianjie[i])
import requests
p_url = 'http://imgwx2.2345.com/dypcimg/img/2/57/sup173972_223x310.jpg'
picture = requests.get(p_url)
# write bytes content 内容
f = open("et2.jpg",'wb')
f.write(picture.content)
f.close()
来源:CSDN
作者:少儿编程侯老师
链接:https://blog.csdn.net/houlaos/article/details/104311091