Python視頻爬蟲實現下載頭條視頻

來源:互聯網
上載者:User
這篇文章主要介紹了Python視頻爬蟲實現下載頭條視頻功能,涉及Python正則匹配、網路傳輸及檔案讀寫等相關操作技巧,需要的朋友可以參考下

本文執行個體講述了Python視頻爬蟲實現下載頭條視頻功能。分享給大家供大家參考,具體如下:

一、需求分析

抓取頭條短視頻

思路:

分析網頁源碼,尋找解析出視頻資源url(查看原始碼,搜mp4)
對該url發起請求,返回位元據
將位元據儲存為視頻格式

視頻連結:
http://video.eastday.com/a/170612170956054127565.html

二、代碼實現

# 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)#沒有這行,列印的結果中文是亂碼resp.encoding = 'utf-8'html = resp.textlink = re.findall(r'var mp4 = "(.*?)";', html)[0]link = 'http:'+linkdest_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

成功下載可以播放~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.