一、自动化采集数据时如何避免滑动验证的阻塞问题
在使用dy刷视频时,如果手速过快就会触发滑动验证码。一般来说,要通过滑块验证需要手动滑动或使用脚本自动滑动,但是如果我们想要自动化采集一些数据,一旦触发滑块验证,程序就会被阻塞,如果不能成功通过滑块验证,程序就会崩溃。然而,根据以往的经验,我们可以在触发滑块验证之前提前通过滑块,并将滑块数据保存下来,这样在后续采集数据时,我们就可以携带之前保存的滑块数据,避免被滑块验证阻塞。
二、如何获取滑块ID:
通过 s_v_web_id 属性或使用相应的 JavaScript 代码来生成
//使用ES6的模板字符串和解构赋值,以及更简洁的方式生成随机字符串
functioncreate_s_v_web_id() {
const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const length = chars.length;
const timestamp = (newDate).getTime().toString(36);
const randomChars = Array.from({ length: 36 }, (_, i) => (i === 8 || i === 13 || i === 18 || i === 23) ? '_' : (i === 14) ? '4' : chars[Math.floor(Math.random() * length)]);
return`verify_${timestamp}_${randomChars.join('')}`;
}
三、如何自动化解决dy的滑块验证码?
使用s_v_web_id获取滑块图片;
识别图片缺口,获取滑动终点的x、y坐标;
生成滑动轨迹;
对滑动轨迹进行加密,生成captchaBody;
发送验证请求,使用captchaBody进行验证;
验证通过后,可以使用s_v_web_id进行后续操作。
如下代码示例:
import random
import requests
def get_captcha(s_v_web_id):
"""
获取滑块图片下载地址
"""
url = "https://verify.snssdk.com/captcha/get"
params = {
"fp": s_v_web_id,
"aid": "6383",
"os_type": "2",
"subtype": "slide",
"tmp": "1639990513276"
}
response = requests.get(url, params=params, verify=False).json()
if response.get('code') == 200:
data = response['data']
img_id, img_url1, img_url2 = data['id'], data['question']['url1'], data['question']['url2']
return img_id, img_url1, img_url2
def get_tracks(distance, _y):
"""
生成滑动轨迹
"""
tracks = []
y, v, t, current = 0, 0, 1, 0
mid = distance * 3 / 4
exceed = random.randint(40, 90)
z = random.randint(30, 150)
while current < (distance + exceed):
a = 2 if current < mid / 2 else (3 if current < mid else -3)
a /= 2
v0 = v
s = v0 * t + 0.5 * a * (t * t)
current += int(s)
v = v0 + a * t
y += random.randint(-3, 3)
z = z + random.randint(5, 10)
tracks.append([min(current, (distance + exceed)), y, z])
while exceed > 0:
exceed -= random.randint(0, 5)
y += random.randint(-3, 3)
z = z + random.randint(5, 9)
tracks.append([min(current, (distance + exceed)), y, z])
return [{'x': x[0], 'y': _y, 'relative_time': x[2]} for x in tracks]
# TODO 需要js生成captchaBody,这个代码就不方便公布了。
def slide_verify(s_v_web_id, captchaBody):
"""
发送验证请求
"""
url = "https://verify.snssdk.com/captcha/verify"
params = {
"os_type": "2",
"fp": s_v_web_id,
"subtype": "slide",
"xx-tt-dd": "qJI7ttpVdGKKbSBvYqmaf0aPo",
}
data = {'captchaBody': captchaBody}
res = requests.post(url=url, params=params, json=data, verify=False).json()
return res['message']
本文旨在分享交流有关滑块验证码的解决思路。如果存在侵权问题,请联系作者进行删除。
另外补充下:我已经完成了dy、宝、宝特价版和鱼APP的分析,并且可以进行交流。我还会补充相关文章,欢迎关注。