How to download headline video by Python crawler
This article introduces the knowledge of "how to download headline video by Python crawler". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. demand analysis
Grab the headline short video
Train of thought:
Analyze the source code of the web page, find and parse the video resource url (view the source code, search mp4)
Initiate a request for the url and return binary data
Save binary data to video format
Second, code implementation
# encoding: utf-8import sysreload (sys) sys.setdefaultencoding ('utf-8') import requestsimport reimport timetime1=time.time () main_url =' http://video.eastday.com/a/170612170956054127565.html'resp = requests.get (main_url) # there is no such line, and the printed result is garbled resp.encoding = 'utf-8'html = resp.textlink = re.findall (r'var mp4 = "(. *?)" ', html) [0] link =' http:'+linkdest_resp = requests.get (link) # Video is a binary data stream Content is the way to get binary data data = dest_resp.content# path and file name path = u'C:/ Zhao Liying. Mp4'f = open (path, 'wb') f.write (data) f.close () time2 = time.time () print u'ok, download completed!' print u 'takes a total of time:' + str (time2-time1) +'s
"D:\ Program Files\ Python27\ python.exe" D:/PycharmProjects/learn2017/testwechat.py
Ok, download complete!
Total time: 3.20499992371s
Process finished with exit code 0
If you download successfully, you can play ~
This is the end of the content of "how to download headline video by Python crawler". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!