最近在做今日头条文章数据抓取的过程中,发现视频地址的获取较为复杂。在源码与浏览器配合下发现对应的解决思路,故此记录一下。
很多人学习python,不知道从何学起。
很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手。
很多已经做案例的人,却不知道如何去学习更加高深的知识。
那么针对这三类人,我给大家提供一个好的学习平台,免费领取视频教程,电子书籍,以及课程的源代码!??¤
QQ群:623406465
1.需要的Python模块
模块主要有requests(或者aiohttp),PyExecJS。 前者是请求文章的源码,后者是Python执行JS代码的依赖库,主要是生成视频地址 12
https://i.snssdk.com/video/urls/1/toutiao/mp4/v0201f800000bub4vq2vtt9a5oknnlp0?callback=tt__video__3e9q4q
接口返回结果中:
同时,该接口中的参数(v0201f800000bub4vq2vtt9a5oknnlp0)在源码中即可找到,可用正则匹配。
可以大胆尝试一下,将main_url值加入生成视频地址的方法中尝试下,另外需要将JS最下面的几个参数加上即:var c = new Array( - 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 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, -1, -1, -1, -1, -1, -1, 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, -1, -1, -1, -1, -1); 1
我用的是JS调试工具(方便调试,检查代码语法),也可用其他方法
结果为:
http://v6-default.ixigua.com/b742fb26ade01b94ae81b46009d87380/5f9944fe/video/tos/cn/tos-cn-ve-31/cb2c3a57a679486eba880ef014c36ca0/?a=2011&br=1368&bt=456&cr=0&cs=0&cv=1&dr=0&ds=1&er=&l=202010281712370101941000395102D7B3&lr=&mime_type=video_mp4&qs=0&rc=M29xcmR3eXQ3eDMzM2kzM0ApZmVkZjo5OGVkNzM6PDozaWdta2gyNnEvc19fLS01Li9zczIuLl41YTFjXi8yMTReYGM6Yw%3D%3D&vl=&vr=
该地址即为视频地址,因此证明了以上的猜想正确,但是该地址参数是有时效的,因此要动态变换。可以自己测试重新生成。
async def get_page_source(url):
browser = None
page = None
try:
browser = await launch(
headless=True,
ignoreHTTPSErrors=True,
handleSIGINT=False,
handleSIGTERM=False,
handleSIGHUP=False,
defaultViewport=None,
args=['--disable-setuid-sandbox',
'--no-sandbox',
'--ignore-certificate-errors',
'--disable-gpu',
'--disable-gpu-sandbox',
'--start-maximized'
]
)
pages = await browser.pages()
page = pages[0]
# 是否启用JS,enabled设为False,则无渲染效果
await page.setJavaScriptEnabled(enabled=True)
await page.setViewport(viewport={'width': 1200, 'height': 800})
await page.evaluateOnNewDocument(
'() =>{ Object.defineProperties(navigator,{ webdriver:{ get: () => false } }) }')
await page.evaluateOnNewDocument("() =>{ Object.defineProperty(navigator, 'plugins', { get: () => [] }) }")
await page.evaluateOnNewDocument(
"() =>{ Object.defineProperty(navigator, 'languages', { get: () => ['zh-CN','zh] }) }")
await page.setUserAgent(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36')
await page.goto(url, {'timeout': 5000, 'waitUntil': 'load'})
page_source = await page.content()
return page_source
except Exception as e:
# app_logger.error('账号:%s, 登录错误:%s' % (username, e))
print(e)
return -1
finally:
if page is not None:
# await page.waitFor(1000)
await page.close()
if browser is not None:
await browser.close()
async def get_data(url, continue_number=0):
"""解析文章源码,提取视频,文字,图片等信息"""
try:
page_source = await get_page_source(url)
# 视频处理,及视频封面
video_message_id_ = re.findall('tt-videoid="(.*?)"', page_source)
video_cover_ = re.findall('tt-poster="(.*?)"', page_source)
if len(video_message_id_) > 0 and len(video_cover_) > 0:
video_message_id = video_message_id_[0]
video_url = await get_video_url_id(video_message_id, url)
video_cover = await download_video_cover(video_cover_[0], url)
except Exception as e:
if continue_number < continue_num:
print(e)
# app_logger.error('function get_data error: %s' % e)
continue_number += 1
video_address = await get_data(url, continue_number)
return video_address
else:
# app_logger.error('function get_data : %s exceed maximum retry' % url)
return -1
async def get_video_url_id(video_id, article_url, continue_number=0):
"""解析视频main_url"""
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/83.0.4103.116 Safari/537.36'}
data_url = 'https://i.snssdk.com/video/urls/1/toutiao/mp4/{}'.format(video_id)
try:
async with aiohttp.ClientSession(connector=TCPConnector(verify_ssl=False), timeout=timeout) as session:
async with session.get(data_url, headers=header) as resp:
response = await resp.json()
if response['message'].strip() == "success":
data = response['data']['video_list']
keys = data.keys()
if 'video_3' in keys:
main_url = data['video_3']['main_url']
video_url = await get_video_url(main_url)
video_url_oss = await download_video(video_url, article_url)
return video_url_oss
elif 'video_3' not in keys and 'video_2' in keys:
main_url = data['video_3']['main_url']
video_url = await get_video_url(main_url)
video_url_oss = await download_video(video_url, article_url)
return video_url_oss
else:
main_url = data['video_3']['main_url']
video_url = await get_video_url(main_url)
video_url_oss = await download_video(video_url, article_url)
return video_url_oss
except Exception as e:
if continue_number < continue_num:
print(e)
# app_logger.error('function get_data error: %s' % e)
continue_number += 1
video_address = await get_data(url, continue_number)
return video_address
else:
# app_logger.error('function get_data : %s exceed maximum retry' % url)
return -1
async def get_video_url(main_url, continue_number=0):
"""获取视频地址,js执行"""
try:
tt = """var c = new Array( - 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 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, -1, -1, -1, -1, -1, -1, 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, -1, -1, -1, -1, -1);
function e(t) {
var e, o, i, r, n, a, s;
for (a = t.length, n = 0, s = ""; a > n;) {
do e = c[255 & t.charCodeAt(n++)];
while (a > n && -1 == e);
if ( - 1 == e) break;
do o = c[255 & t.charCodeAt(n++)];
while (a > n && -1 == o);
if ( - 1 == o) break;
s += String.fromCharCode(e << 2 | (48 & o) >> 4);
do {
if (i = 255 & t.charCodeAt(n++), 61 == i) return s;
i = c[i]
} while ( a > n && - 1 == i );
if ( - 1 == i) break;
s += String.fromCharCode((15 & o) << 4 | (60 & i) >> 2);
do {
if (r = 255 & t.charCodeAt(n++), 61 == r) return s;
r = c[r]
} while ( a > n && - 1 == r );
if ( - 1 == r) break;
s += String.fromCharCode((3 & i) << 6 | r)
}
return s
}"""
js = execjs.compile(tt)
result = js.call('e', main_url)
return result
except Exception as e:
if continue_number < continue_num:
# app_logger.error('function get_video_url error: %s' % e)
continue_number += 1
video_address = await get_video_url(main_url, continue_number)
return video_address
else:
# app_logger.error('function get_video_url exceed maximum retry')
return -1
总结该项目是JS反爬的一种,相对来说不是很复杂,未采用JS代码混淆,参数加密等,查找方向上障碍不是很多,后期碰到复杂点的会继续更新分享的