基本信息
源码名称:Python爬取抖音UP主的全部视频
源码大小:5.01KB
文件格式:.py
开发语言:Python
更新时间:2022-03-30
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
程序运行时,需要输入抖音UP主的URL。例如:在电脑端,通过官网搜索,可以找到:XXX的主页,https://www.douyin.com/user/xxxxxxxxxxxxxxxxxx
源代码没有做容错处理,也没有针对抖音的反爬措施进行处理,因此运行中途可能会意外中止。
# 定义函数get_video_ids(author_url),返回UP主全部短视频的ID的列表
# 参数author_url:抖音UP主的主页
# 例如,XXX的主页 https://www.douyin.com/user/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
def get_video_ids(author_url):
ids = list()
count = 0
retry = 0
n = 0
flag = True
chrome_option = webdriver.ChromeOptions()
chrome_option.add_argument('headless') # 静默模式
driver = webdriver.Chrome(options=chrome_option)
driver.get(author_url)
while flag and retry <= 15:
driver.execute_script("window.scrollBy(0,2000)") # scrollBy(x, y),JavaScript中操作内容滚动指定的像素数
n = n 1
time.sleep(2)
html_source = driver.page_source
items = etree.HTML(html_source).xpath("//li[@class='ECMy_Zdt']")
count_items = len(items)
print("操作页面内容滚动{0:0>3}次后,获取视频ID{1:0>4}个。".format(n, count_items))
if count_items != count:
count = count_items
else:
if retry < 15:
retry = retry 1
else:
flag = False
print("已经达到可获取视频ID的最大数量,开始逐个获取视频ID:\n")
for item in items:
video_id = item.xpath("a/@href")[0].split("/")[-1]
print("获取短视频ID:{}".format(video_id))
ids.append(video_id)
return ids