抖音没有提供API接口,因此需要使用Selenium模拟浏览器操作进行爬取。

以下是一个基本的爬取抖音评论的Python代码示例:

# 导入Selenium模块
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# 设置抖音视频链接和评论数
url = "https://www.douyin.com/video/6977107611092509208"
comment_num = 100

# 创建浏览器对象
driver = webdriver.Chrome()

# 打开抖音网页
driver.get(url)

# 模拟滚动操作以加载评论
for i in range(1, comment_num // 10):
    driver.find_element_by_tag_name('body').send_keys(Keys.END)
    time.sleep(1)

# 获取评论文本
comments = driver.find_elements_by_xpath('//div[@class="comment-list"]/div//span[contains(@class, "comment-text")]')
for comment in comments:
    print(comment.text)

# 关闭浏览器
driver.quit()

注意:该代码只能爬取前100条评论,如果需要爬取更多评论,需要修改comment_num的值,并增加滚动次数。另外,由于抖音网页结构可能会发生变化,代码执行时可能会出现错误,需要根据实际情况进行修改。


原文地址: https://www.cveoy.top/t/topic/pWA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录