Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to download headline video by Python crawler

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report