首先声明,本文档只是记录学习一下Python爬虫, 并不支持读者利用以下内容下载vip视频。
爬取视频代码如下:
import requests
from multiprocessing import Pool
import os
def download(i):
#打印当前进程ID
#print(os.getpid())
url = ""
# 视频格式解析,jx.618g.com:支持爱奇艺、腾讯视频、优酷、芒果等的视频解析,ts是高清视频的格式
# https://vip.okokbo.com/20171214/YxCchIeq/800kb/hls/M4kekPq3832000.ts
# https://vip.okokbo.com/20171214/YxCchIeq/800kb/hls/M4kekPq38321924.ts
# 观测规律:<1000时只有3位 %03d >=1000 %04d。注意%03d 3表示占位符位数,0表示不足3位用0补
if i < 1000:
url = "https://vip.okokbo.com/20171214/YxCchIeq/800kb/hls/M4kekPq3832%03d.ts" % i
else:
url = "https://vip.okokbo.com/20171214/YxCchIeq/800kb/hls/M4kekPq3832%04d.ts" % i
#content:获取字节流内容,text获取unicode内容
response = requests.get(url).content;
with open('./video/{}'.format(url[-10:]),'wb') as f:
f.write(response)
if __name__ == '__main__':
po = Pool(10)
for i in range(1924):
po.apply_async(download, args=(i,))
po.close()
po.join()
#后续操作
#windows
#命令提示符cmd进入到download下载目录:如, cd D:/workspace/person/python-project/vip视频爬虫/video
#执行命令:copy /b *.ts 名字.mp4