1、首先需要你去百度网盘开放平台申请一个控制台权限
2、开通后新建应用
3、应用新建完之后你会得到:
这是我的代码:
start_web.py
class IndexHandler(tornado.web.RequestHandler): def get(self): list = [] list.append({'title':'百度网盘device', 'url':'/baidu/device'}) list.append({'title':'百度网盘token', 'url':'/baidu/token'}) list.append({'title':'百度网盘用户信息', 'url':'/baidu/userinfo'}) list.append({'title':'百度网盘文件信息', 'url':'/baidu/filelist'}) list.append({'title':'百度网盘图片信息', 'url':'/baidu/imglist'}) self.render('index.html', list=list) if __name__ == "__main__": handlers=[ (r"/", IndexHandler), (r"/baidu/device", baiduwangpan.DeviceHandler), (r"/baidu/token", baiduwangpan.TokenHandler), (r"/baidu/userinfo", baiduwangpan.UserinfoHandler), (r"/baidu/filelist", baiduwangpan.FilelistHandler), (r"/baidu/imglist", baiduwangpan.ImglistHandler), ],
baiduwangpan.py
#coding=utf-8 import tornado.web import tornado.httpclient from tornado import gen import urllib import json import datetime import time import os,sys import requests from awscli.customizations.emr.constants import FALSE deviceinfo = {} Appkey = '' #填入你自己的appkey Secretkey = '' #填入你自己的Secretkey class DeviceHandler(tornado.web.RequestHandler): def get(self): payload = {} url1 = 'https://openapi.baidu.com/oauth/2.0/device/code?response_type=device_code&client_id=%s&scope=basic,netdisk' % (Appkey) response = requests.request("GET", url1, data = payload) responsestr = response.text.encode('utf8') #print(type(responsestr)) try: file = open("./deviceinfo.txt", 'w', encoding='utf-8') file.write(str(responsestr, 'utf-8')) file.close() except Exception as e: pass deviceinfo = json.loads(str(responsestr, 'utf-8')) #print(deviceinfo) self.write("%s<br />\n\n" % url1) self.write("<img src='"+deviceinfo['qrcode_url']+"' width=200 height=200 /><br />\n\n") #self.write(deviceinfo) class TokenHandler(tornado.web.RequestHandler): def get(self): file = open("./deviceinfo.txt", 'r', encoding='utf-8') responsestr = file.read() file.close() deviceinfo = json.loads(responsestr) #print(deviceinfo) payload = {} url2 = 'https://openapi.baidu.com/oauth/2.0/token?grant_type=device_token&code=%s&client_id=%s&client_secret=%s' % (deviceinfo['device_code'], Appkey, Secretkey) response = requests.request("GET", url2, data = payload) responsestr = response.text.encode('utf8') try: file = open("./tokeninfo.txt", 'w', encoding='utf-8') file.write(str(responsestr, 'utf-8')) file.close() except Exception as e: pass tokeninfo = json.loads(str(responsestr, 'utf-8')) self.write("%s<br />\n\n" % url2) self.write(tokeninfo) class UserinfoHandler(tornado.web.RequestHandler): def get(self): file = open("./tokeninfo.txt", 'r', encoding='utf-8') responsestr = file.read() file.close() tokeninfo = json.loads(responsestr) #print(deviceinfo) payload = {} url3 = 'https://pan.baidu.com/rest/2.0/xpan/nas?method=uinfo&access_token=%s' % tokeninfo['access_token'] response = requests.request("GET", url3, data = payload) responsestr = response.text.encode('utf8') #self.write(url3) userinfo = json.loads(str(responsestr, 'utf-8')) #self.write(userinfo) self.write("<img src='"+userinfo['avatar_url']+"' width=200 height=200 /><br />\n\n") self.write(userinfo['netdisk_name']+"\n\n") class FilelistHandler(tornado.web.RequestHandler): def get(self): page = int(self.get_argument("page", 1)) dir = self.get_argument("dir", "/") file = open("./tokeninfo.txt", 'r', encoding='utf-8') responsestr = file.read() file.close() tokeninfo = json.loads(responsestr) #print(deviceinfo) limit = 30 start = (page - 1) * limit payload = {} url3 = 'https://pan.baidu.com/rest/2.0/xpan/file?method=list&access_token=%s&dir=%s&web=1&start=%s&limit=%s&order=size&desc=0' % (tokeninfo['access_token'], dir, start, limit) response = requests.request("GET", url3, data = payload) responsestr = response.text.encode('utf8') #self.write(url3) filelist = json.loads(str(responsestr, 'utf-8')) #print(filelist) count = len(filelist['list']) prepage = " <a href=\"/baidu/filelist?dir=%s&page=%s\">上一页</a> " % (dir, page-1) nextpage = " <a href=\"/baidu/filelist?dir=%s&page=%s\">下一页</a> " % (dir, page+1) self.write("<h2>当前目录:%s,文件数量:%s, %s, %s</h2>" % (dir, count, prepage, nextpage)) self.write("<table border=\"1\" bordercolor=\"#003366\" style=\"border-collapse:collapse;\" width=100%>") self.write("<tr bgcolor=\"#CC9966\"><td>顺序</td><td>id</td><td>文件名</td><td>大小</td></tr>") k = 0 for dir in filelist['list']: bgcolor = '' if k % 2 == 1: bgcolor = '#F0FFFF' k += 1 if dir['isdir'] == 1: filename = "<a href=\"/baidu/filelist?dir="+dir['path']+"\">"+dir['server_filename']+"</a>" else: filename = dir['server_filename'] try: filename = "<img src=\""+dir['thumbs']['url3']+"\">" except: pass self.write("<tr bgcolor=\""+bgcolor+"\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>" % (k, dir['fs_id'], filename, dir['size'])) self.write("</table>") class ImglistHandler(tornado.web.RequestHandler): def get(self): page = int(self.get_argument("page", 1)) dir = self.get_argument("dir", "/图片") file = open("./tokeninfo.txt", 'r', encoding='utf-8') responsestr = file.read() file.close() tokeninfo = json.loads(responsestr) #print(deviceinfo) limit = 30 start = (page - 1) * limit payload = {} url3 = 'https://pan.baidu.com/rest/2.0/xpan/file?method=imagelist&parent_path=%s&page=%s&access_token=%s&web=1&order=size&desc=0&num=%s' % (dir, page, tokeninfo['access_token'], limit) response = requests.request("GET", url3, data = payload) responsestr = response.text.encode('utf8') #self.write(url3) filelist = json.loads(str(responsestr, 'utf-8')) count = len(filelist['info']) #print(filelist) prepage = " <a href=\"/baidu/filelist?dir=%s&page=%s\">上一页</a> " % (dir, page-1) nextpage = " <a href=\"/baidu/filelist?dir=%s&page=%s\">下一页</a> " % (dir, page+1) self.write("<h2>当前目录:%s,图片数量:%s, %s, %s</h2>" % (dir, count, prepage, nextpage)) if filelist['errno'] != 0: pass else: self.write("<table border=\"1\" bordercolor=\"#003366\" style=\"border-collapse:collapse;\" width=100%>") self.write("<tr bgcolor=\"#CC9966\"><td>顺序</td><td>id</td><td>文件类型</td><td>预览</td><td>目录</td><td>修改时间</td><td>大小</td></tr>") k = 0 for dir in filelist['info']: bgcolor = '' if k % 2 == 1: bgcolor = '#F0FFFF' k += 1 category = dir['category'] icon = "<img src=\""+dir['thumbs']['url3']+"\">" self.write("<tr bgcolor=\""+bgcolor+"\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>" % (k, dir['fs_id'], category, icon, dir['isdir'], dir['server_mtime'], dir['size'])) self.write("</table>")
展示效果:
标签:baidu,百度网,python,responsestr,self,write,file,操作,dir From: https://www.cnblogs.com/xuxiaobo/p/17116522.html