Python爬取V聊视频从信息入库到下载

1.视频url的入库(库和表需要自己去创建代码稍做修改就行)
#!/usr/bin/python # -*- coding: utf-8 -*-

import json
import urllib
import requests
from bj.models import ksVideo
from bj.models import Video

url = "http://v3.vliao3.xyz/v31/smallvideo/one"
headers = {
        'interfaceVersion':'3.1.1',
        'interfaceSource':'android',
        'Content-Type':'application/x-www-form-urlencoded',
        'Content-Length':'67',
        'Host':'v3.vliao3.xyz',
        'Connection':'Keep-Alive',
        'Accept-Encoding':'gzip',
        'User-Agent':'okhttp/3.4.1',
    }


def start():
    download_ks_video(74)

def download_ks_video():
    for i in range(2500,3500):
        data = "userId=440049&userKey=1968f35f6638708142688a33baca48cc&&videoId="+str(i)+"&vid=-1"
        req=requests.post(url=url,data=data,headers=headers).content
        # print(req)
        req_json=json.loads(req)
        video=req_json.get('data')
        if video is None:
            continue
        print(video)
        user_id='440049'
        video_id=video.get('id')
        video_url=video.get('url')
        video_desc=video.get('title')

        Video.objects.get_or_create(
            video_id=video_id,
            defaults={
                'user_id': user_id,
                'video_url': video_url,
                'video_desc': video_desc
            }
        )


    print("下载完成!!")



上图代码中的userId+userKey是隔一段时间会发生变化(所以需要重新抓包查看这里我推荐使用charles去抓包)
userId=440049&userKey=1968f35f6638708142688a33baca48cc


2.对视频进行下载

import os
import requests
from bs4 import BeautifulSoup
import threading
from bj.models import Video
from bj.script import downloadUrl
from bj.script import bjdownloadvideo

def down_video():
    # 獲取數據庫中還沒有被下載的視頻
    videos=Video.objects.filter(is_download=False)[:100]
    if videos is None:
        return


    #path = "/home/facelive/Downloads/videos/"

    # 硬盘路径
    path = "/media/facelive/Elements/videos/"

    if os._exists(path):
        print("-->該pathnot存在!!")
        os.makedirs(path)
    for video in videos :
        video_id=video.video_id
        video_url=video.video_url
        path_url=os.path.join(path,'%s'%(video_id)+".mp4")

        thread=threading.Thread(target=save_video,args=(video_url,path_url))
        thread.start()

        # 修改已經下載的視頻的狀態
        video.is_download = True
        video.save()
        print("视频"+str(video_id)+"已开始下载!!!")
    print("《《《《所有視頻下載线程已经启动!!!!》》》")
# 正式下頻載保存視
def save_video(url,path):
    response=bjdownloadvideo.get_response(url)

    with open(path,'wb') as f:
        f.write(response)


猜你喜欢

转载自blog.csdn.net/wsywb111/article/details/79127185