ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

没想到上面好看的跳舞小姐姐蛮多的,【Python爬虫】采集微博视频数据

2021-12-03 14:31:22  阅读:240  来源: 互联网

标签:小姐姐 headers Python json url 微博 video tv data


前言

随时随地发现新鲜事!微博带你欣赏世界上每一个精彩瞬间,了解每一个幕后故事。分享你想表达的,让全世界都能听到你的心声!今天我们通过python去采集微博当中好看的视频!

没错,今天的目标是微博数据采集,爬的是那些好看的小姐姐视频

对于本篇文章有疑问的同学可以加【资料白嫖、解答交流群:910981974】

知识点

  • requests
  • pprint

开发环境

  • 版 本:python 3.8
    -编辑器:pycharm 2021.2

爬虫原理

  • 作用:批量获取互联网数据(文本, 图片, 音频, 视频)
  • 本质:一次次的请求与响应

 

案例实现

1. 导入所需模块

import requests
import pprint

 

2. 找到目标网址

打开开发者工具,选中Fetch/XHR,选中数据所在的标签,找到目标所在url

 https://www.weibo.com/tv/api/component?page=/tv/channel/4379160563414111/editor

 

3. 发送网络请求

headers = {
    'cookie': '',
    'referer': 'https://weibo.com/tv/channel/4379160563414111/editor',
    'user-agent': '',
}
data = {
    'data': '{"Component_Channel_Editor":{"cid":"4379160563414111","count":9}}'
}
url = 'https://www.weibo.com/tv/api/component?page=/tv/channel/4379160563414111/editor'
json_data = requests.post(url=url, headers=headers, data=data).json()

 

4. 获取数据

json_data_2 = requests.post(url=url_1, headers=headers, data=data_1).json()

 

5. 筛选数据

dict_urls = json_data_2['data']['Component_Play_Playinfo']['urls']
video_url = "https:" + dict_urls[list(dict_urls.keys())[0]]
print(title + "\t" + video_url)

 

6. 保存数据

video_data = requests.get(video_url).content
with open(f'video\\{title}.mp4', mode='wb') as f:
    f.write(video_data)
print(title, "爬取成功................")

 

完整代码

import requests
import pprint

headers = {
    'cookie': '添加自己的',
    'referer': 'https://weibo.com/tv/channel/4379160563414111/editor',
    'user-agent': '',
}
data = {
    'data': '{"Component_Channel_Editor":{"cid":"4379160563414111","count":9}}'
}
url = 'https://www.weibo.com/tv/api/component?page=/tv/channel/4379160563414111/editor'
json_data = requests.post(url=url, headers=headers, data=data).json()
print(json_data)

ccs_list = json_data['data']['Component_Channel_Editor']['list']
next_cursor = json_data['data']['Component_Channel_Editor']['next_cursor']
for ccs in ccs_list:
    oid = ccs['oid']
    title = ccs['title']
    data_1 = {
        'data': '{"Component_Play_Playinfo":{"oid":"' + oid + '"}}'
    }
    url_1 = 'https://weibo.com/tv/api/component?page=/tv/show/' + oid
    json_data_2 = requests.post(url=url_1, headers=headers, data=data_1).json()
    dict_urls = json_data_2['data']['Component_Play_Playinfo']['urls']
    video_url = "https:" + dict_urls[list(dict_urls.keys())[0]]
    print(title + "\t" + video_url)

    video_data = requests.get(video_url).content
    with open(f'video\\{title}.mp4', mode='wb') as f:
        f.write(video_data)
    print(title, "爬取成功................")

 

标签:小姐姐,headers,Python,json,url,微博,video,tv,data
来源: https://www.cnblogs.com/qshhl/p/15637804.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有