Home  >  Article  >  Backend Development  >  Python video crawler implements downloading headline videos

Python video crawler implements downloading headline videos

不言
不言Original
2018-05-07 13:49:152044browse

This article mainly introduces the Python video crawler to implement the function of downloading headline videos, involving Python regular matching, network transmission, file reading and writing and other related operating skills. Friends in need can refer to the following

This article describes the examples Python video crawler implements the function of downloading headline videos. Share it with everyone for your reference, the details are as follows:

1. Demand analysis

Capture headline short videos

Ideas:

Analyze the web page source code, find and parse the video resource url (view source code, search mp4)
Initiate a request for the url and return binary data
Save the binary data as video format

Video link:
http://video.eastday.com/a/170612170956054127565.html

2. Code Implementation

# 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, download completed!
Total time taken: 3.20499992371s
Process finished with exit code 0

Successful download can be played~

Related recommendations:

videocapture library produces python video high-speed transmission program

##

The above is the detailed content of Python video crawler implements downloading headline videos. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn