import requests
import os
import json
from moviepy.editor import VideoFileClip, AudioFileClip
from bs4 import BeautifulSoup

# 数据的抓取
base_url = 'http://www.zkk78.com/index.php/user/ajax_ulog/?ac=set&mid=1&id=4721&sid=1&nid=1&type=4'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.55',
    'Referer': 'http://www.zkk78.com/dongmanplay/1660-1-1.html',
    'Cookie': 'first_h_kp=1689927842258; count_h_kp=1; first_m_kp=1689927842260; count_m_kp=1; __51cke__=; Hm_lvt_38ea8ed97fbe7c334fcc1878c579e5e0=1689927815; Hm_lvt_c11e70df18184f7263176ce90c8a9cc3=1689927817; PHPSESSID=2pnhv3rla9asc5i0l248i8dl83; user_id=20620; user_name=2143552649; group_id=2; group_name=%E9%BB%98%E8%AE%A4%E4%BC%9A%E5%91%98; user_check=b93515cf840072f8df045b0842e31513; user_portrait=%2Fstatic%2Fimages%2Ftouxiang.png; XLA_CI=43a5b7a031caf14cbacec3c121269bcf; first_h_kp=1689930340869; count_h_kp=1; first_m_kp=1689930340870; count_m_kp=1; history=%5B%7B%22name%22%3A%22%E4%BD%A0%E7%9A%84%E5%90%8D%E5%AD%97%22%2C%22pic%22%3A%22https%3A%2F%2Fcdn.yinghuazy.xyz%2Fupload%2Fvod%2F20210219-1%2F5e3b65ef9e6c582f09784d027cfb1923.jpg%22%2C%22link%22%3A%22%2Fdongmanplay%2F1660-1-1.html%22%2C%22part%22%3A%22%E5%85%A8%E9%9B%86%E6%97%A5%E8%AF%AD%22%7D%2C%7B%22name%22%3A%22%E5%9B%9E%E5%A4%8D%E6%9C%AF%E5%A3%AB%E7%9A%84%E9%87%8D%E6%9D%A5%E4%BA%BA%E7%94%9F%22%2C%22pic%22%3A%22https%3A%2F%2Fpic.rmb.bdstatic.com%2Fbjh%2F377f268028bb09e80035191cd9f15c58.jpeg%22%2C%22link%22%3A%22%2Fdongmanplay%2F4721-1-1.html%22%2C%22part%22%3A%22%E7%AC%AC01%E9%9B%86%22%7D%5D; Hm_lpvt_c11e70df18184f7263176ce90c8a9cc3=1689930351; Hm_lpvt_38ea8ed97fbe7c334fcc1878c579e5e0=1689930351; __tins__21589017=%7B%22sid%22%3A%201689930340967%2C%20%22vd%22%3A%206%2C%20%22expires%22%3A%201689932161945%7D; __51laig__=15',

}

response = requests.get(base_url,headers=headers) #第一次请求
print(response.status_code)
data = response.text
print(data)

#数据解析, jsonload转换为python格式
json_data = json.loads(data)
print(json_data)
json_list = [json_data['info']] # 将字典放入列表中
print(json_list)

for item in json_list:
    video_title = item['title'] + '.mp4'
    video_url = item['play_url']
    print(video_title,video_url)

    print('正在下载:',video_title)
    #第二次请求
    video_data = requests.get(video_url,headers=headers).content
    with open(r'./视频/'+video_title,'wb') as f:
        f.write(video_data)
        print('下载完成
')

代码解析

  1. 导入库: 首先,导入必要的库:

    • requests:用于发送 HTTP 请求
    • os:用于操作文件系统
    • json:用于解析 JSON 数据
    • moviepy.editor:用于处理视频文件
    • bs4:用于解析 HTML 页面
  2. 定义基础 URL 和请求头: 定义目标网站的 URL 和请求头,包括 User-Agent、Referer 和 Cookie。

  3. 发送请求: 使用 requests.get 函数发送 GET 请求到目标 URL,并获取响应。

  4. 解析 JSON 数据: 使用 json.loads 函数将响应的文本内容解析为 Python 字典。

  5. 处理视频信息: 从 JSON 数据中提取视频标题和 URL,并打印出来。

  6. 下载视频: 使用 requests.get 函数获取视频数据,并使用 with open 语句将视频数据写入本地文件。

代码改进

  • 将字典数据放入列表中,解决 TypeError: string indices must be integers 错误。
  • 使用 with open 语句打开文件,确保文件在使用后自动关闭。
  • 增加了对代码的注释,使其更易于理解。

总结

这段代码演示了如何使用 Python 爬虫技术抓取网站视频数据并下载,包括使用 requests 库发送请求、json 库解析数据、以及 moviepy 库处理视频文件。通过改进代码,确保代码的正确性和可读性。

Python 爬虫实战:抓取视频数据并下载

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

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