本文實(shí)例講述了Python視頻爬蟲(chóng)實(shí)現(xiàn)下載頭條視頻功能。分享給大家供大家參考,具體如下:
一、需求分析
抓取頭條短視頻
思路:
分析網(wǎng)頁(yè)源碼,查找解析出視頻資源url(查看源代碼,搜mp4)
對(duì)該url發(fā)起請(qǐng)求,返回二進(jìn)制數(shù)據(jù)
將二進(jìn)制數(shù)據(jù)保存為視頻格式
視頻鏈接:
http://video.eastday.com/a/170612170956054127565.html
二、代碼實(shí)現(xiàn)
# 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)#沒(méi)有這行,打印的結(jié)果中文是亂碼resp.encoding = 'utf-8'html = resp.textlink = re.findall(r'var mp4 = "(.*?)";', html)[0]link = 'http:'+linkdest_resp = requests.get(link)#視頻是二進(jìn)制數(shù)據(jù)流,content就是為了獲取二進(jìn)制數(shù)據(jù)的方法data = dest_resp.content#保存數(shù)據(jù)的路徑及文件名path = u'C:/趙麗穎.mp4'f = open(path, 'wb')f.write(data)f.close()time2 = time.time()print u'ok,下載完成!'print u'總共耗時(shí):' + str(time2 - time1) + 's"D:/Program Files/Python27/python.exe" D:/PycharmProjects/learn2017/testwechat.py
ok,下載完成!
總共耗時(shí):3.20499992371s
Process finished with exit code 0
成功下載可以播放~

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選