本文實例講述了Python視頻爬蟲實現下載頭條視頻功能。分享給大家供大家參考,具體如下:
一、需求分析
抓取頭條短視頻
思路:
分析網頁源碼,查找解析出視頻資源url(查看源代碼,搜mp4)
對該url發起請求,返回二進制數據
將二進制數據保存為視頻格式
視頻鏈接:
http://video.eastday.com/a/170612170956054127565.html
二、代碼實現
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# encoding: utf-8 import sys reload (sys) sys.setdefaultencoding( 'utf-8' ) import requests import re import time time1 = time.time() main_url = 'http://video.eastday.com/a/170612170956054127565.html' resp = requests.get(main_url) #沒有這行,打印的結果中文是亂碼 resp.encoding = 'utf-8' html = resp.text link = re.findall(r 'var mp4 = "(.*?)";' , html)[ 0 ] link = 'http:' + link dest_resp = requests.get(link) #視頻是二進制數據流,content就是為了獲取二進制數據的方法 data = dest_resp.content #保存數據的路徑及文件名 path = u 'C:/趙麗穎.mp4' f = open (path, 'wb' ) f.write(data) f.close() time2 = time.time() print u 'ok,下載完成!' print u '總共耗時:' + str (time2 - time1) + 's |
"D:\Program Files\Python27\python.exe" D:/PycharmProjects/learn2017/testwechat.py
ok,下載完成!
總共耗時:3.20499992371s
Process finished with exit code 0
成功下載可以播放~
希望本文所述對大家Python程序設計有所幫助。
原文鏈接:https://blog.csdn.net/u013421629/article/details/73134804