一、封装一个函数来识别要解析的类型
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19// 获取类型 get_type(){ if(this.url.match(/http[s]?://v.douyin.com/[^ ]+/) != null){ console.log("识别到【dy】链接") return "dy" } else if(this.url.match(/http[s]?://v.kuaishou.com/[^ ]+/) != null){ console.log("识别到【ks】链接") return "ks" } else if(this.url.match(/http[s]?://xhslink.com/[^ ]+/) != null){ console.log("识别到【xhs】链接") return "xhs" } else{ console.log("未识别到链接类型,请输入正确的链接") return null } }
二、在初始化方法中写入本实例共用的数据
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14// 初始化方法 constructor() { this.token = "Z1QljZOZiT4NTG" // token // 请求地址数组对象 this.req_urls = { dy: "http://api.txapi.cn/v1/parse_short_video/dy", ks: "http://api.txapi.cn/v1/parse_short_video/ks", xhs: "http://api.txapi.cn/v1/parse_short_video/xhs", } this.url = '' // 要解析的地址 this.type = '' // 用来存储识别到的类型 }
三、封装一个“万能解析”的方法
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32// 万能解析 parse_video(){ axios({ url: this.req_urls[this.type], method: 'POST', headers: { 'Content-Type': "application/x-www-form-urlencoded" }, responseType: 'json', data: { token: this.token, url: this.url } }) .then(resp => { // 校验是否解析成功 if(resp.data.code != 200 && resp.data.msg != "OK"){ console.log("解析失败") } else{ // 获取到解析后的数据 const data = resp.data.data console.log(data) var type = data.type // 类型:1视频 2图片集 var title = data.title // 标题 var cover_url = data.cover_url // 封面地址 var video_url = data.video_url // 无水印视频地址 var imgs = data.imgs // 无水印图片数组 } }) }
废话不多说 直接上完整代码????
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98const axios = require('axios') class Parse{ // 初始化方法 constructor() { this.token = "Z1QljZOZiT4NTG" // token // 请求地址数组对象 this.req_urls = { dy: "http://api.txapi.cn/v1/parse_short_video/dy", ks: "http://api.txapi.cn/v1/parse_short_video/ks", xhs: "http://api.txapi.cn/v1/parse_short_video/xhs", } this.url = '' // 要解析的地址 this.type = '' // 用来存储识别到的类型 } // 万能解析 parse_video(){ axios({ url: this.req_urls[this.type], method: 'POST', headers: { 'Content-Type': "application/x-www-form-urlencoded" }, responseType: 'json', data: { token: this.token, url: this.url } }) .then(resp => { // 校验是否解析成功 if(resp.data.code != 200 && resp.data.msg != "OK"){ console.log("解析失败") } else{ // 获取到解析后的数据 const data = resp.data.data console.log(data) var type = data.type // 类型:1视频 2图片集 var title = data.title // 标题 var cover_url = data.cover_url // 封面地址 var video_url = data.video_url // 无水印视频地址 var imgs = data.imgs // 无水印图片数组 } }) } // 获取类型 get_type(){ if(this.url.match(/http[s]?://v.douyin.com/[^ ]+/) != null){ console.log("识别到【dy】链接") return "dy" } else if(this.url.match(/http[s]?://v.kuaishou.com/[^ ]+/) != null){ console.log("识别到【ks】链接") return "ks" } else if(this.url.match(/http[s]?://xhslink.com/[^ ]+/) != null){ console.log("识别到【xhs】链接") return "xhs" } else{ console.log("未识别到链接类型,请输入正确的链接") return null } } // 使用正则区分要解析的链接是哪个平台的【dy、ks、xhs】 run(url){ // 1、把url保存给实例变量【方便后期使用】 this.url = url // 1、获取类型 this.type = this.get_type(); if(!this.type){ return } // 2、调用万能解析 this.parse_video() } } if(__filename === process.mainModule.filename) { // new一个Parse对象 const p = new Parse() // 调用run方法 p.run("https://v.douyin.com/hoDBW9H") p.run("https://v.kuaishou.com/C75B2q") p.run("http://xhslink.com/fKihbj") }
最后
以上就是狂野西装最近收集整理的关于去水印最新教程的全部内容,更多相关去水印最新教程内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复