
在爬取抖音博主全部视频时,遇到翻页问题,如何通过程序自动完成翻页并加载后续视频链接?
翻页方式
抖音视频翻页有两种方式:
解决方法
使用最大 id 方式实现翻页:
获取初始页面中的视频链接后,提取页面中最新的视频 id(max_id)。
在随后的请求中,将 max_id 作为查询参数,获取下一页的视频链接。
不断重复此过程,直到获取所有视频链接。
代码实现
在你的代码中,在 get_video_url() 函数中添加以下内容:
if not video_url:
return
max_id = None
while True:
try:
url_new = 'https://www.douyin.com/user/MS4wLjABAAAAp3rtDfotN7-mHjDIr0XR2XJ5g0C1DIVAuJYgBHJYX-xJLZgoHvfN0r0yAWTLybn7'
if max_id:
url_new += f'?max_cursor={max_id}'
r = requests.get(url=url_new, headers=headers)
html = BeautifulSoup(r.text, 'html.parser')
result = html.find_all('a', 'B3AsdZT9 chmb2GX8 UwG3qaZV')
if not result:
print("Finished")
break
for item in result:
get_video(item['href'])
max_id = item['href'][-19:]
except Exception as e:
print("解析失败")
print(e)
break这样,代码将自动翻页,并获取该抖音博主全部视频链接。
以上就是抖音视频爬虫如何自动完成翻页?的详细内容,更多请关注php中文网其它相关文章!
抖音极速版是一款可以领现金的短视频app,看视频、拍视频、邀好友,均可获得专属金币红包,助您能轻松赚钱!感兴趣的小伙伴快来保存下载体验吧!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号