您的位置:首页 > 编程语言 > Python开发

一篇帖子教会你如何获取网盘数据

u4226814 2019-10-09 18:09 92 查看 https://my.oschina.net/u/42268

随着版权越来越重,做个电影站、磁力站、小说站已经越来越难,而网盘搜索的话由于背靠百度大哥,还相对好一些,但是百度压力也是越来越大,这个领域获取数据其实也基本不太可能。好在有些团队凭借着长期的数据储备,积累了很多相关数据,可以采用直接调用数据的形式实现,目前做的比较好的当属小葱计算,这是小编最近才发现的一个平台,也大概看了下demo,确实很容易,以python为例:

# -*- coding: utf-8 -*-
# flake8: noqa
__author__ = 'wukong'

import urllib
from urllib import urlencode

#配置您申请的appKey和openId
app_key="***"
open_id="***"

"""
request_url 请求地址
params 请求参数
method 请求方法

"""
def request_content(request_url,params,method):
params = urlencode(params)

if method and method.lower() =="get":
f = urllib.urlopen("%s?%s" % (request_url, params))
else:
f = urllib.urlopen(request_url, params)

content = f.read()
print content

def main():

domain="http://api.xiaocongjisuan.com/"
servlet="data/skydriverdata/get"
method="get"
request_url=domain+servlet

#字典
params ={}
params["appKey"]=app_key
params["openId"]=open_id

#变动部分
params["q"]="a"
params["currentPage"]=1
params["pageSize"]=20

request_content(request_url,params,method)

if __name__ == '__main__':
main()

java 为例:

package com.xiaocongjisuan.module.example;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class Application {

public static final String DEF_CHATSET = "UTF-8";
public static final int DEF_CONN_TIMEOUT = 30000;
public static final int DEF_READ_TIMEOUT = 30000;
public static String userAgent =  "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";

//配置您申请的appKey和openId
public static final String APP_KEY ="yours";
public static final String OPEN_ID ="yours";

//将map型转为请求参数型
public static String urlEncode(Map<String,Object> params) {

if(params==null){return "";};

StringBuilder sb = new StringBuilder();
for (Map.Entry<String,Object> i : params.entrySet()) {
try {
sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
String r=sb.toString();
if(r.endsWith("&")){
r = r.substring(0,r.length()-1);
}
return r;
}

/**
*
* @param requestUrl 请求地址
* @param params 请求参数
* @param method 请求方法
* @return 请求结果
* @throws Exception
*/
public static String requestContent(String requestUrl, Map<String,Object> params,String method) throws Exception {

HttpURLConnection conn = null;
BufferedReader reader = null;
String rs = null;
try {

//组装请求链接
StringBuffer sb = new StringBuffer();

if(method!=null&&method.equalsIgnoreCase("get")){
requestUrl = requestUrl+"?"+urlEncode(params);
}

//默认get
URL url = new URL(requestUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");

if(method!=null&&method.equalsIgnoreCase("post")){
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
}

//参数配置
conn.setRequestProperty("User-agent", userAgent);
conn.setUseCaches(false);
conn.setConnectTimeout(DEF_CONN_TIMEOUT);
conn.setReadTimeout(DEF_READ_TIMEOUT);
conn.setInstanceFollowRedirects(false);
conn.connect();

if (params!= null && method.equalsIgnoreCase("post")) {
try {
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes(urlEncode(params));
} catch (Exception e) {
e.printStackTrace();
}
}

//读取数据
InputStream is = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sb.append(strRead);
}
rs = sb.toString();

} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
if (conn != null) {
conn.disconnect();
}
}
return rs;
}

public static void main(String[] args) throws Exception{

String domain="http://api.xiaocongjisuan.com/";
String servlet="data/skydriverdata/get";
String method="get";

String requestUrl=domain+servlet;
Map<String,Object> params=new HashMap<String,Object>();
params.put("appKey",APP_KEY);
params.put("openId",OPEN_ID);

//变动部分
params.put("q","a");
params.put("currentPage",1);
params.put("pageSize",20);

String result=requestContent(requestUrl,params,method);
System.out.println(result);
}
}

平台的文档参数都写的比较清晰,具体可以网盘数据查看,返回结果有json/xml两种形式:

{
"data": {
"result": [{
"id": 15229,
"title": "19.<span style='color:red'>STM</span><span style='color:red'>32</span>+<span style='color:red'>UCOSIII</span><span style='color:red'>开发</span>",
"url": "https://pan.baidu.com/s/1jHVppmm",
"size": "0 B",
"shareTime": 1544196520000,
"shareUser": "men****gwc",
"isDir": "1",
"originId": 1,
"originName": "百度网盘",
"categoryId": 6,
"categoryName": "文件夹(压缩)",
"password": "1h6h",
"acqTime": 1544196520000,
"content": "文件|4,程序源码|3,ALIENTEK战舰<span style='color:red'>STM</span><span style='color:red'>32</span>F<span style='color:red'>1</span> V3<span style='color:red'>开发</span>板原理图|2,ALIENTEK战舰<span style='color:red'>STM</span><span style='color:red'>32</span>F<span style='color:red'>1</span> V3<span style='color:red'>开发</span>板视频教程|<span style='color:red'>1</span>,ALIENTEK战舰<span style='color:red'>STM</span><span style='color:red'>32</span>F<span style='color:red'>1</span> V3<span style='color:red'>开发</span>板入门资料|联系我们.pdf|<span style='color:red'>STM</span><span style='color:red'>32</span>F<span style='color:red'>1</span><span style='color:red'>开发</span>指南-库函数版本_V3.<span style='color:red'>1</span> .pdf|<span style='color:red'>STM</span><span style='color:red'>32</span>F<span style='color:red'>1</span><span style='color:red'>开发</span>指南-寄存器版本_V3.<span style='color:red'>1</span> .pdf|<span style='color:red'>STM</span><span style='color:red'>32</span>F<span style='color:red'>1</span> UCOS<span style='color:red'>开发</span>手册_V2.0.pdf|<span style='color:red'>STM</span><span style='color:red'>32</span>F<span style='color:red'>1</span> LWIP<span style='color:red'>开发</span>手册(DM9000版)_V<span style='color:red'>1</span>.<span style='color:red'>1</span>.pdf|<span style='color:red'>STM</span><span style='color:red'>32</span>F<span style='color:red'>1</span> EMWIN<span style='color:red'>开发</span>手册_V2.0.pdf|01",
"uk": "2568959939",
"shareId": "3221440608",
"fileCount": 12,
"fromTable": 5,
"uniqueKey": "15a3c21502524cde3dca0602265d0de2",
"shortUrl": "1jHVppmm",
"extendFiles": [{
"fsId": "679418566138045",
"serverFilename": "文件",
"size": "0 B"
}, {
"fsId": "276424880413983",
"serverFilename": "4,程序源码",
"size": "0 B"
}, {
"fsId": "512606928244026",
"serverFilename": "3,ALIENTEK战舰<span style='color:red'>STM</span><span style='color:red'>32</span>F<span style='color:red'>1</span> V3<span style='color:red'>开发</span>板原理图",
"size": "2.9 GB"
}, {
"fsId": "490793316644212",
"serverFilename": "2,ALIENTEK战舰<span style='color:red'>STM</span><span style='color:red'>32</span>F<span style='color:red'>1</span> V3<span style='color:red'>开发</span>板视频教程",
"size": "3.2 GB"
}, {
"fsId": "449624001387403",
"serverFilename": "<span style='color:red'>1</span>,ALIENTEK战舰<span style='color:red'>STM</span><span style='color:red'>32</span>F<span style='color:red'>1</span> V3<span style='color:red'>开发</span>板入门资料",
"size": "3.7 GB"
}, {
"fsId": "934962552301526",
"serverFilename": "联系我们.pdf",
"size": "283 MB"
}, {
"fsId": "151892956852061",
"serverFilename": "<span style='color:red'>STM</span><span style='color:red'>32</span>F<span style='color:red'>1</span><span style='color:red'>开发</span>指南-库函数版本_V3.<span style='color:red'>1</span> .pdf",
"size": "8.1 MB"
}, {
"fsId": "277550860315906",
"serverFilename": "<span style='color:red'>STM</span><span style='color:red'>32</span>F<span style='color:red'>1</span><span style='color:red'>开发</span>指南-寄存器版本_V3.<span style='color:red'>1</span> .pdf",
"size": "2.0 GB"
}, {
"fsId": "675377464375217",
"serverFilename": "<span style='color:red'>STM</span><span style='color:red'>32</span>F<span style='color:red'>1</span> UCOS<span style='color:red'>开发</span>手册_V2.0.pdf",
"size": "3.3 GB"
}, {
"fsId": "686926257121399",
"serverFilename": "<span style='color:red'>STM</span><span style='color:red'>32</span>F<span style='color:red'>1</span> LWIP<span style='color:red'>开发</span>手册(DM9000版)_V<span style='color:red'>1</span>.<span style='color:red'>1</span>.pdf",
"size": "3.7 GB"
}, {
"fsId": "829790437997820",
"serverFilename": "<span style='color:red'>STM</span><span style='color:red'>32</span>F<span style='color:red'>1</span> EMWIN<span style='color:red'>开发</span>手册_V2.0.pdf",
"size": "12.2 MB"
}, {
"fsId": "383158876469280",
"serverFilename": "01",
"size": "0 B"
}]
}],
"amount": 1,
"totalPage": 1,
"time": 0.20107889,
"currentPage": 1
},
"errorMessage": "成功",
"errorCode": 0,
"error": "success"
}

小编觉得这个接口做的不错,省了不少事情,没必要再去研究爬虫,用资源吸引粉丝、推广小程序还是非常好的方式,觉得不错就给我投票点赞吧。

标签:  Java Python