抖音用户视频爬虫案例(2020)
本文代码已过期、思路有一定参考价值。
2021新版加密的文章链接:
https://blog.csdn.net/weixin_43582101/article/details/114291365
抖音分享页面
之前有相关文章介绍了分享页面的抓取和sign值的生成。要看详细方法可点击下方链接。
本篇文章主要是说一下最新生成sign值的代码,最后会贴在文章下方。
新版的分享页面上增加了webdriver的验证,并且今年谷歌新版本浏览器停止了对开发者模式隐藏webdervier的支持。
导致了之前的 add_experimental_option(‘excludeSwitches’, [‘enable-automation’])失效了。方法在下面文章
抖音视频sign值生成
页面链接: https://www.amemv.com/share/user/102064772608
102064772608就是分享页面需要的ID。
页面上的数据:
下面是我提取出的数据 :
运行环境:
Package | Version |
---|---|
python | 3.6.8 |
Google Chrome | 80.0.3987.163 |
Selenium | 3.141.0 |
需要安装selenium和对应的谷歌驱动。
Code:
import re,os,json,requests
PRO_DIR = os.path.dirname(os.path.abspath(__file__))
less_list = ['d','V','J','t','m','L','R','l','X','b','Z','B','r','3','v','N','D','P','9','1','H','7','T','z','j','f','x','p','h','5','F','.']
add_list = ['a','K','W','M','u','Q','S','A','e','U','O','Y','I','0','k','E','y','c','w','2','s','8','4','6','C','o','g','q','-','i','G','n',]
douyinShareId = '102064772608' # 分享页面ID
ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36'
headers = {"user-agent":ua}
share_url = 'https://www.amemv.com/share/user/{}'.format(douyinShareId)
doc = requests.get(share_url,headers=headers).text
# 提取 signature 获取用户视频数据
p1 = r'<script>tac=\'(?P<tac>[\W\w]{150,}?)\'</script>'
pattern1 = re.compile(p1)
p2 = r'dytk ?: ?\'(?P<dytk>[0-9a-z]*?)\''
pattern2 = re.compile(p2)
tac = pattern1.search(doc).group('tac')
dytk = pattern2.search(doc).group('dytk')
s_tac = "tac=\'{}\'".format(tac)
s1 = """
<!DOCTYPE html>
<html style="font-size: 50px;"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>抖音_signature破解</title>
</head>
<body></body>
</html>
<script type="text/javascript">
"""
s2 = """
!function(t) {
if (t.__M = t.__M || {},
!t.__M.require) {
var e, n, r = document.getElementsByTagName("head")[0], i = {}, o = {}, a = {}, u = {}, c = {}, s = {}, l = function(t, n) {
if (!(t in u)) {
u[t] = !0;
var i = document.createElement("script");
if (n) {
var o = setTimeout(n, e.timeout);
i.onerror = function() {
clearTimeout(o),
n()
}
;
var a = function() {
clearTimeout(o)
};
"onload"in i ? i.onload = a : i.onreadystatechange = function() {
("loaded" === this.readyState || "complete" === this.readyState) && a()
}
}
return i.type = "text/javascript",
i.src = t,
r.appendChild(i),
i
}
}, f = function(t, e, n) {
var r = i[t] || (i[t] = []);
r.push(e);
var o, a = c[t] || c[t + ".js"] || {}, u = a.pkg;
o = u ? s[u].url || s[u].uri : a.url || a.uri || t,
l(o, n && function() {
n(t)
}
)
};
n = function(t, e) {
"function" != typeof e && (e = arguments[2]),
t = t.replace(/\.js$/i, ""),
o[t] = e;
var n = i[t];
if (n) {
for (var r = 0, a = n.length; a > r; r++)
n[r]();
delete i[t]
}
}
,
e = function(t) {
if (t && t.splice)
return e.async.apply(this, arguments);
t = e.alias(t);
var n = a[t];
if (n)
return n.exports;
var r = o[t];
if (!r)
throw "[ModJS] Cannot find module `" + t + "`";
n = a[t] = {
exports: {}
};
var i = "function" == typeof r ? r.apply(n, [e, n.exports, n]) : r;
return i && (n.exports = i),
n.exports && !n.exports["default"] && Object.defineProperty && Object.isExtensible(n.exports) && Object.defineProperty(n.exports, "default", {
value: n.exports
}),
n.exports
}
,
e.async = function(n, r, i) {
function a(t) {
for (var n, r = 0, h = t.length; h > r; r++) {
var p = e.alias(t[r]);
p in o ? (n = c[p] || c[p + ".js"],
n && "deps"in n && a(n.deps)) : p in s || (s[p] = !0,
l++,
f(p, u, i),
n = c[p] || c[p + ".js"],
n && "deps"in n && a(n.deps))
}
}
function u() {
if (0 === l--) {
for (var i = [], o = 0, a = n.length; a > o; o++)
i[o] = e(n[o]);
r && r.apply(t, i)
}
}
"string" == typeof n && (n = [n]);
var s = {}
, l = 0;
a(n),
u()
}
,
e.resourceMap = function(t) {
var e, n;
n = t.res;
for (e in n)
n.hasOwnProperty(e) && (c[e] = n[e]);
n = t.pkg;
for (e in n)
n.hasOwnProperty(e) && (s[e] = n[e])
}
,
e.loadJs = function(t) {
l(t)
}
,
e.loadCss = function(t) {
if (t.content) {
var e = document.createElement("style");
e.type = "text/css",
e.styleSheet ? e.styleSheet.cssText = t.content : e.innerHTML = t.content,
r.appendChild(e)
} else if (t.url) {
var n = document.createElement("link");
n.href = t.url,
n.rel = "stylesheet",
n.type = "text/css",
r.appendChild(n)
}
}
,
e.alias = function(t) {
return t.replace(/\.js$/i, "")
}
,
e.timeout = 5e3,
t.__M.define = n,
t.__M.require = e
}
}(this)
__M.define("douyin_falcon:node_modules/byted-acrawler/dist/runtime", function(l, e) {
Function(function(l) {
return 'e(e,a,r){(b[e]||(b[e]=t("x,y","x "+e+" y")(r,a)}a(e,a,r){(k[r]||(k[r]=t("x,y","new x[y]("+Array(r+1).join(",x[y]")(1)+")")(e,a)}r(e,a,r){n,t,s={},b=s.d=r?r.d+1:0;for(s["$"+b]=s,t=0;t<b;t)s[n="$"+t]=r[n];for(t=0,b=s=a;t<b;t)s[t]=a[t];c(e,0,s)}c(t,b,k){u(e){v[x]=e}f{g=,ting(bg)}l{try{y=c(t,b,k)}catch(e){h=e,y=l}}for(h,y,d,g,v=[],x=0;;)switch(g=){case 1:u(!)4:f5:u((e){a=0,r=e;{c=a<r;c&&u(e[a]),c}}(6:y=,u((y8:if(g=,lg,g=,y===c)b+=g;else if(y!==l)y9:c10:u(s(11:y=,u(+y)12:for(y=f,d=[],g=0;g<y;g)d[g]=y.charCodeAt(g)^g+y;u(String.fromCharCode.apply(null,d13:y=,h=delete [y]14:59:u((g=)?(y=x,v.slice(x-=g,y:[])61:u([])62:g=,k[0]=65599*k[0]+k[1].charCodeAt(g)>>>065:h=,y=,[y]=h66:u(e(t[b],,67:y=,d=,u((g=).x===c?r(g.y,y,k):g.apply(d,y68:u(e((g=t[b])<"<"?(b--,f):g+g,,70:u(!1)71:n72:+f73:u(parseInt(f,3675:if(){bcase 74:g=<<16>>16g76:u(k[])77:y=,u([y])78:g=,u(a(v,x-=g+1,g79:g=,u(k["$"+g])81:h=,[f]=h82:u([f])83:h=,k[]=h84:!085:void 086:u(v[x-1])88:h=,y=,h,y89:u({e{r(e.y,arguments,k)}e.y=f,e.x=c,e})90:null91:h93:h=0:;default:u((g<<16>>16)-16)}}n=this,t=n.Function,s=Object.keys||(e){a={},r=0;for(c in e)a[r]=c;a=r,a},b={},k={};r'.replace(/[-]/g, function(e) {
return l[15 & e.charCodeAt(0)]
})
}("v[x++]=v[--x]t.charCodeAt(b++)-32function return ))++.substrvar .length(),b+=;break;case ;break}".split("")))()('gr$Daten Иb/s!l y͒yĹg,(lfi~ah`{mv,-n|jqewVxp{rvmmx,&effkx[!cs"l".Pq%widthl"@q&heightl"vr*getContextx$"2d[!cs#l#,*;?|u.|uc{uq$fontl#vr(fillTextx$$龘ฑภ경2<[#c}l#2q*shadowBlurl#1q-shadowOffsetXl#$$limeq+shadowColorl#vr#arcx88802[%c}l#vr&strokex[ c}l"v,)}eOmyoZB]mx[ cs!0s$l$Pb<k7l l!r&lengthb%^l$1+s$jl s#i$1ek1s$gr#tack4)zgr#tac$! +0o![#cj?o ]!l$b%s"o ]!l"l$b*b^0d#>>>s!0s%yA0s"l"l!r&lengthb<k+l"^l"1+s"jl s&l&z0l!$ +["cs\\'(0l#i\\'1ps9wxb&s() &{s)/s(gr&Stringr,fromCharCodes)0s*yWl ._b&s o!])l l Jb<k$.aj;l .Tb<k$.gj/l .^b<k&i"-4j!+& s+yPo!]+s!l!l Hd>&l!l Bd>&+l!l <d>&+l!l 6d>&+l!l &+ s,y=o!o!]/q"13o!l q"10o!],l 2d>& s.{s-yMo!o!]0q"13o!]*Ld<l 4d#>>>b|s!o!l q"10o!],l!& s/yIo!o!].q"13o!],o!]*Jd<l 6d#>>>b|&o!]+l &+ s0l-l!&l-l!i\\'1z141z4b/@d<l"b|&+l-l(l!b^&+l-l&zl\\'g,)gk}ejo{cm,)|yn~Lij~em["cl$b%@d<l&zl\\'l $ +["cl$b%b|&+l-l%8d<@b|l!b^&+ q$sign ', [Object.defineProperty(e, "__esModule", {
value: !0
})])
});
dycs = __M.require("douyin_falcon:node_modules/byted-acrawler/dist/runtime")
signc = dycs.sign(&&&)
document.title=signc
document.write(signc)
</script>
"""
s2 = s2.replace('&&&', douyinShareId)
with open(os.path.join(PRO_DIR, 'douyin_sign.html'), 'w', encoding='utf-8') as fw:
fw.write(s1 + s_tac + s2)
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument('start-maximized')
option.add_argument('--disable-dev-shm-usage')
option.add_argument('--disable-extensions')
option.add_argument('--disable-gpu')
option.add_argument('--no-sandbox')
option.add_argument('headless')
option.add_argument('disable-infobars')
option.add_argument('--user-agent={}'.format(ua))
driver = webdriver.Chrome(chrome_options=option)
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
""" })
driver.get('file:///' + PRO_DIR + '/douyin_sign.html')
sig = driver.title
driver.quit()
if sig[-2] in less_list:
flag = '-1'
sig = sig[:-2] + chr(ord(sig[-2])-1) + sig[-1:]
elif sig[-2] in add_list:
flag = '+1'
sig = sig[:-2] + chr(ord(sig[-2])+1) + sig[-1:]
else:
print("新字符:",sig) # 新字符默认取 ASCII +1
flag = '+1'
sig = sig[:-2] + chr(ord(sig[-2])+1) + sig[-1:]
#detail_url = 'https://www.iesdouyin.com/web/api/v2/aweme/post/?user_id={}&sec_uid=&count=21&max_cursor=0&aid=1128&_signature={}&dytk={}'.format(douyinShareId,sig, dytk)
detail_url = 'https://www.amemv.com/web/api/v2/aweme/post/?user_id={}&sec_uid=&count=21&max_cursor=0&aid=1128&_signature={}&dytk={}'.format(douyinShareId,sig, dytk)
data = json.loads(requests.get(detail_url,headers=headers).text)
if len(data['aweme_list']) == 0:
sig = re.findall('&_signature=(.*?)&',detail_url)[0]
if flag=='-1':
new_sig = sig[:-2] + chr(ord(sig[-2])+2) + sig[-1:]
else:
new_sig = sig[:-2] + chr(ord(sig[-2])-2) + sig[-1:]
url = detail_url.replace(sig,new_sig)
data = requests.get(url,headers={headers}).json()
if data['status_code'] == 0:
item = []
for awlist in data['aweme_list']:
video_list = {}
statistics = awlist['statistics']
video = awlist['video']
digg_count = statistics.get('digg_count') # 点赞数
play_count = statistics.get('play_count') # 播放数
forward_count = statistics.get('forward_count') # 转发数
comment_count = statistics.get('comment_count') # 评论数
aweme_id = awlist['aweme_id'] # 视频id
desc = awlist.get('desc') # 简介
cover = video['cover']['url_list'][0] # 视频封面
download_addr = video['download_addr']['url_list'] # 下载地址
share_count = statistics.get('share_count') # 分享数
duration = video.get('duration') # 持续时间
video_list['likeNum'] = digg_count
video_list['forwardNum'] = forward_count
video_list['commentNum'] = comment_count
video_list['mid'] = aweme_id
video_list['content'] = desc
video_list['videoPic'] = cover
video_list['videoUrl'] = download_addr[0].replace('play/?','playwm/?')
video_list['playNum'] = play_count
item.append(video_list)
print(item)
- 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
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
抖音无水印视频
当前保存的 videoUrl 链接是经过处理后的,如果需要无水印的视频。
把 videoUrl 链接中的 playwm 替换成 play 。删除 watermark=1 。
然后把浏览器切换为手机版,再打开链接。
使用程序下载的时候需要使用手机版的 User-Agent。
文章来源: blog.csdn.net,作者:考古学家lx,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_43582101/article/details/105946844
- 点赞
- 收藏
- 关注作者
评论(0)