import urllib.request import json import os #导入os模块 import requests #导入requests 模块 class BeautifulPicture(): def __init__(self): #类的初始化操作 self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'} #给请求指定一个请求头来模拟chrome浏览器 self.folder_path = 'D:\demo\BeautifulPicture' #设置图片要存放的文件目录 #利用urllib2获取网络数据 def registerUrl(self): print('开始创建文件夹') self.mkdir(self.folder_path) #创建文件夹 print('开始切换文件夹') os.chdir(self.folder_path) #切换路径至上面创建的 try:
# 请求的接口 url ="https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=0%2C0&fp=detail&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=0&lpn=0&st=undefined&word=%E6%A0%A1%E8%8A%B1&z=0&ic=undefined&s=undefined&se=&tab=0&width=undefined&height=undefined&face=undefined&istype=0&qc=&nc=&fr=&simics=&srctype=&bdtype=0&rpstart=0&rpnum=0&cs=3447792407%2C2312708615&catename=&cardserver=&tabname=&pn=0&rn=30&gsm=0&1535795026719=" data = urllib.request.urlopen(url).read() return data except Exception as e: print(e) #写入文件 def save_img(self, url, name): ##保存图片 print(url) img = self.request(url) file_name = name + '.jpg' print('开始保存图片') with open(file_name, 'ab') as f: f.write(img.content) # print(file_name,'图片保存成功!') #解析从网络上获取的JSON数据 def praserJsonFile(self, jsonData): value = json.loads(jsonData) data = value['data'] print(data) for i in data: url = i['middleURL'] name = i['fromPageTitleEnc'] self.save_img(url, name) def request(self, url): #返回网页的response r = requests.get(url, headers=self.headers) # 像目标url地址发送get请求,返回一个response对象。有没有headers参数都可以。 return r def mkdir(self, path): ##这个函数创建文件夹 path = path.strip() isExists = os.path.exists(path) if not isExists: print('创建名字叫做', path, '的文件夹') os.makedirs(path) print('创建成功!') else: print(path, '文件夹已经存在了,不再创建') if __name__ == "__main__": beauty = BeautifulPicture() #创建类的实例 data = beauty.registerUrl() # jsonFile(data) beauty.praserJsonFile(data)