以此URl举个例子http://www.meipai.com/media/816057957
- 页面分析
通过分析美拍播放页面的源代码,可以看到,播放页面的html代码中的有一个mate标签(property属性值为og:video:url),该值就是页面的视频链接,只不过是经过加密后的
- 使用Java语言来自动获取美拍真实加密过的url地址,Crawl类
// 根据url请求html页面
public static String clawer2(String myurl) throws Exception{
URL urlmy = new URL(myurl);
HttpURLConnection con = (HttpURLConnection) urlmy.openConnection();
con.setFollowRedirects(true);
con.setInstanceFollowRedirects(false);
con.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
String s = "";
StringBuffer sb = new StringBuffer("");
while ((s = br.readLine()) != null) {
sb.append(s+"\r\n");
}
return sb.toString();
}
// 通过Jsoup包来解析html页面
public static String parsehtml(String url) throws Exception {
String htmlContent = Crawl.clawer2(url);
//使用jSoup解析里头的内容
//就像操作html doc文档对象一样操作网页中的元素
Document doc = Jsoup.parse(htmlContent);
Element span = doc.select("head > meta:nth-child(16)").first();
System.out.println(span.attr("content"));
return span.attr("content");// 获取加密后的视频url地址
}
- 解密加密字符串,解密后即可得到视频真实URL地址
/**
* 解密加密过的字符串
* Created by lihan on 2017/7/29.
*/
public class ParseUrl {
private Map<String, String> dict1;
private Map<String, String[]> dict2;
// 获得16进制数,该数用来分割字符串
public Map<String,String> getHex(String param1){
dict1 = new HashMap<String, String>();
String cstr = param1.substring(4);//str
String[] splitStr = param1.substring(0,4).split("");
String hex = "";
for (int i=3; i >= 0; i--){
hex = hex + splitStr[i];
}
dict1.put("str", cstr);
dict1.put("hex", hex);
return dict1;
}
// 获取正确的字符串,解析16进制数
public Map<String, String[]> getDecimal(String param1){
dict2 = new HashMap<String, String[]>();
// loc2是用来分割字符串的索引标识,转换16进制
String loc2 = String.valueOf(Integer.parseInt(param1,16));
String[] pre = loc2.substring(0,2).split("");//dict1.put("loc2", loc2.substring(0,2));
String[] tail = loc2.substring(2).split("");
dict2.put("pre", pre);
dict2.put("tail", tail);
return dict2;
}
// 分割字符串
public String substr(String param1, String[] param2) {
String loc3 = param1.substring(0, Integer.parseInt(param2[0]));//param2 = pu.getDec(pa2).get("pre")
String loc4 = param1.substring(Integer.parseInt(param2[0]), Integer.parseInt(param2[0])+Integer.parseInt(param2[1]));
return loc3 + param1.substring(Integer.parseInt(param2[0])).replace(loc4, "");
}
// 获取分割的位置
public String[] getPosition(String param1, String[] param2){
param2[0] = String.valueOf(param1.length() - Integer.parseInt(param2[0]) - Integer.parseInt(param2[1]));
return param2;
}
// 程序入口
public static void main(String[] args) throws Exception {
ParseUrl pu = new ParseUrl();
// 获取html中的加密字符串
String code = Crawl.parsehtml("http://www.meipai.com/media/816057957");
Map<String, String> dict2 = pu.getHex(code);
Map<String,String[]> dict3 = pu.getDecimal(dict2.get("hex"));
String str4 = pu.substr(dict2.get("str"), dict3.get("pre"));
BASE64Decoder base64 = new BASE64Decoder();
byte[] url = base64.decodeBuffer(pu.substr(str4, pu.getPosition(str4, dict3.get("tail"))));
// 视频真实的url地址
System.out.println(new String(url));
}
}
- 运行后即可得到真实的视频url
- python版本可以参考:
需要安装requests库,安装命令:
pip install requests
# coding=utf-8
# Created by lihan on 2017/7/29.
import base64
import requests
from pyquery import PyQuery as pq
class Decode:
def getHex(self,param1):
return {
'str': param1[4:],
'hex': ''.join(list(param1[:4])[::-1]),
}
def getDecimal(self, param1):
loc2 = str(int(param1, 16))
print(loc2)#
return {
'pre': list(loc2[:2]),
'tail': list(loc2[2:]),
}
def substr(self, param1, param2):
loc3 = param1[0: int(param2[0])]
loc4 = param1[int(param2[0]): int(param2[0]) + int(param2[1])]
print("loc4",loc4)
return loc3 + param1[int(param2[0]):].replace(loc4, "")
def getPosition(self,param1, param2):
param2[0] = len(param1) - int(param2[0]) - int(param2[1])
return param2
def decode(self, code):
dict2 = self.getHex(code)
dict3 = self.getDecimal(dict2['hex'])
print("dict3", dict3['pre']) #
str4 = self.substr(dict2['str'], dict3['pre'])
return base64.b64decode(self.substr(str4, self.getPosition(str4, dict3['tail'])))
def crawl_video_url(self, url):
response = requests.get(url).content
d = pq(response)
code= d('meta[property="og:video:url"]').attr('content')
result = self.decode(code)
print(result)
if __name__ == '__main__':
url = "http://www.meipai.com/media/816057957"//任意播放页面的url
Decode().crawl_video_url(url)
网友评论