You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium Python在Google Colab中滚动TikTok无法加载新视频问题

问题描述

使用Python Selenium打开TikTok用户主页时,目标URL可正常访问,初始加载完成后的页面源码、首批已加载视频数据均可正常获取。但执行滚动操作、等待固定时长后再次获取页面源码,内容与初始加载结果完全一致,无新加载的视频内容。
先后尝试两种滚动实现方案均未生效:

  • 循环判断页面滚动高度,自动滚动至页面底部后等待加载,若滚动高度无变化则终止循环
  • 单次执行滚动到底部操作后等待10秒再获取页面源码
    运行环境为Google Colab,使用apt源安装的chromium-chromedriver。
已完成修正
  • 已将代码中误用的driver变量名修正为实际使用的浏览器实例变量wd
复现代码

初始循环滚动方案

from selenium import webdriver
from selenium.webdriver.common.by import By
import re
import json
from bs4 import BeautifulSoup
import time

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

wd = webdriver.Chrome('chromedriver',options=options)
wd.get("https://www.tiktok.com/@tiktok")
time.sleep(20)

SCROLL_PAUSE_TIME = 20

last_height = wd.execute_script("return document.body.scrollHeight")

while True:
    wd.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(SCROLL_PAUSE_TIME)
    new_height = wd.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height

print(wd.page_source)

单次滚动测试代码

wd.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(10)
print(wd.page_source)

环境安装命令

# install chromium, its driver, and selenium
!apt-get update
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
!pip install selenium
问题原因
  • 滚动对象错误:TikTok用户主页的视频列表采用独立div容器实现局部滚动,并非挂载在全局window/body节点下,直接调用window.scrollTo不会触发列表容器绑定的滚动监听事件,无法触发懒加载逻辑。
  • 反爬检测拦截:默认无头模式启动的Chromium会在UA中携带HeadlessChrome标识,且navigator.webdriver属性默认为true,会被TikTok反爬机制识别,返回仅包含首批内容的降级页面,后续所有交互操作都不会得到响应。
  • 浏览器版本兼容问题:Colab通过apt源安装的Chromium版本比官方正式版落后3个以上大版本,存在部分JS API兼容问题,无法正常触发前端绑定的交互事件。
解决方案

1. 补全浏览器启动参数,隐藏自动化特征

启动Chrome时新增以下参数,同时在页面加载前注入JS覆盖自动化标识:

options = webdriver.ChromeOptions()
options.add_argument('--headless=new')  # 启用新版无头模式,UA与正常Chrome完全一致
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
# 手动指定正式版Chrome UA,避免被识别
options.add_argument('--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36')

wd = webdriver.Chrome('chromedriver', options=options)
# 注入JS覆盖webdriver自动化标识
wd.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
    'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
})

2. 定位实际滚动容器,针对容器执行滚动操作

等待视频列表容器加载完成后,直接操作容器滚动,不要操作全局window:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wd.get("https://www.tiktok.com/@tiktok")
# 等待视频列表容器加载完成
scroll_container = WebDriverWait(wd, 20).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, 'div[data-e2e="user-post-item-list"]'))
)

SCROLL_PAUSE_TIME = 3
last_height = wd.execute_script("return arguments[0].scrollHeight", scroll_container)

while True:
    # 滚动列表容器到底部
    wd.execute_script("arguments[0].scrollTo(0, arguments[0].scrollHeight);", scroll_container)
    time.sleep(SCROLL_PAUSE_TIME)
    new_height = wd.execute_script("return arguments[0].scrollHeight", scroll_container)
    # 二次校验,避免网络延迟导致提前终止循环
    if new_height == last_height:
        time.sleep(2)
        new_height = wd.execute_script("return arguments[0].scrollHeight", scroll_container)
        if new_height == last_height:
            break
    last_height = new_height

3. 替换浏览器驱动版本

若以上操作执行后仍无法加载内容,卸载apt安装的chromium-chromedriver,改用webdriver-manager自动匹配安装与Chrome版本对应的驱动,解决版本兼容问题。


内容的提问来源于stack exchange,提问作者unknown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 20:18:29