前言

抖音短视频相信大家都听过,也不陌生对吧!可以看到海量的短视频 ,涵盖了各大行业。个人觉得抖音有毒,刷着刷着根本停不下来,一看时间就是凌晨3 、4点 。今天带大家爬取抖音网页版的视频数据!一睹为快吧

本篇文章内容:

1、系统分析网页性质

2、正则提取数据(难点)

3 、海量音频数据保存

环境介绍:

python 3.6
pycharm
requests
re

爬虫的一般思路

1、分析目标网页 ,确定爬取的url路径,headers参数

2、发送请求 -- requests 模拟浏览器发送请求,获取响应数据

3 、解析数据 -- 正则表达式

4、保存数据 -- 保存在目标文件夹中

 

步骤:

1、导入工具

base_url = 'http://douyin.bm8.com.cn/d_1.html'
headers = {
    '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'}

 

2 、分析目标网页 ,确定爬取的url路径,headers参数

base_url = 'http://douyin.bm8.com.cn/d_1.html'
headers = {
    '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'}

 

 

 

3 、发送请求 -- requests 模拟浏览器发送请求,获取响应数据

response = requests.get(url=base_url, headers=headers)
html_data = response.text

 

4、解析数据 -- 正则表达式

pattern = re.compile('onclick="open1\(\'(.*?)\',\'(.*?)\',\'\'\)')
result = pattern.findall(html_data)
print(result)

 

5、构建一个for循环

for page in range(8, 10):
    print('===================正在取第{}页数据================='.format(page))
    # 1	、分析目标网页	,确定爬取的url路径,headers参数
    base_url = 'http://douyin.bm8.com.cn/d_{}.html'.format(page)
    headers = {
        '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'}

 

6、处理文件名非法字符

def change_title(title):
    pattern = re.compile(r"[\/\\\:\*\?\"\<\>\|]")  # '/ \ : * ? " < > |'
    new_title = re.sub(pattern, "_", title)  # 替换为下划线
    return new_title

 

7、保存数据 -- 保存在目标文件夹中

for title, url in result:
        # 请求抖音视频数据
        data = requests.get(url=url, headers=headers).content

        new_title = change_title(title)
        with open('videos\\' + new_title + '.mp4', mode='wb') as f:
            f.write(data)
            print('保存完成:', title)

 

 
文章来源于网络,如有侵权请联系站长QQ61910465删除
本文版权归QU快排Www.seoGurubLog.com 所有,如有转发请注明来出,竞价开户托管,seo优化请联系QQ▲61910465