Python爬蟲爬取影片的詳細介紹

不言
發布: 2018-09-19 17:27:20
原創
22168 人瀏覽過

這篇文章帶給大家的內容是關於Python爬蟲爬取影片的詳細介紹,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

最近在寫一個應用,需要收集微博上一些熱門的視頻,像這些小視頻一般都來自秒拍,微拍,美拍和新浪視頻,而且沒有下載的選項,所以只能動腦想想辦法了。

第一步

  分析網頁原始碼。例如:http://video.weibo.com/show?fid=1034:0988e59a12e5178acb7f23adc3fe5e97,右鍵查看源碼,一般影片都是mp4後綴,搜尋發現沒有,但是有的直接就能看到了例如美拍的影片。

第二步

  抓包,分析請求與回傳。這個也可以透過強大的chrome實現,還是上面的例子,右鍵->審查元素->NetWork,然後F5刷新網頁 
Python爬蟲爬取影片的詳細介紹

發現有很多請求,只能一條一條的分析了,其實影片格式就是那幾種mp4,flv,avi了,一下就能看到了,複製到瀏覽器中打開,果然就是我們想要的下載連結了。 
Python爬蟲爬取影片的詳細介紹

第三步驟

  分析下載連結和影片連結的法則。即http://video.weibo.com/show?fid=1034:0988e59a12e5178acb7f23adc3fe5e97與xxx.mp4的關係。這個又需要分析網頁源碼了,其實可以注意上面那個以.m3u8後綴的鏈接,m3u8記錄了一個索引純文本文件,打開它時播放軟體並不是播放它,而是根據它的索引找到對應的音視頻文件的網路位址進行線上播放,打開看,裡面確實記錄著我們想要的下載連結。而且.m3u8字尾的連結就在網頁源碼中。
Python爬蟲爬取影片的詳細介紹

總結

  經過前三步驟的分析,獲取視頻下載鏈接的思路就是先從網頁源碼中獲取.m3u8後綴的鏈接,下載該文件,從裡面得到視頻下載鏈接,最後下載視頻就好了

源碼

#sinavideo.py
#coding=utf-8
import os
import re
import urllib2
import urllib 
from common import Common
class SinaVideo():

    URL_PIRFIX = "http://us.sinaimg.cn/"
    def getM3u8(self,html):
        reg = re.compile(r'list=([\s\S]*?)&fid')
        result = reg.findall(html)
        return result[0]


    def getName(self,url):
         return url.split('=')[1]

    def getSinavideoUrl(self,filepath):
        f = open(filepath,'r')
        lines = f.readlines()
        f.close()
        for line in lines:
            if line[0] !='#':
                return line

    def download(self,url,filepath):
        #获取名称
        name = self.getName(url)
        html = Common.getHtml(url)
        m3u8 = self.getM3u8(html)
        Common.download(urllib.unquote(m3u8),filepath,name + '.m3u8')
        url = self.URL_PIRFIX + self.getSinavideoUrl(filepath+name+'.m3u8')
        Common.download(url,filepath,name+'.mp4')
登入後複製
#common.py
#coding=utf-8
import urllib2
import os
import re


class Common():
    #  获取网页源码
    @staticmethod
    def getHtml(url):
        html = urllib2.urlopen(url).read()
        print  "[+]获取网页源码:"+url
        return html

    # 下载文件
    @staticmethod
    def download(url,filepath,filename):
        headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Charset': 'UTF-8,*;q=0.5',
            'Accept-Encoding': 'gzip,deflate,sdch',
            'Accept-Language': 'en-US,en;q=0.8',
            'User-Agent': 'Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Mobile Safari/537.36'
        }
        request = urllib2.Request(url,headers = headers);
        response = urllib2.urlopen(request)
        path = filepath + filename
        with open(path,'wb') as output:
            while True:
                buffer = response.read(1024*256);
                if not buffer:
                    break
                # received += len(buffer)
                output.write(buffer)

        print "[+]下载文件成功:"+path

    @staticmethod
    def isExist(filepath):
        return os.path.exists(filepath)

    @staticmethod
    def createDir(filepath):
         os.makedirs(filepath,0777)
登入後複製

調用方式:

 url = "http://video.weibo.com/show?fid=1034:0988e59a12e5178acb7f23adc3fe5e97"sinavideo = SinaVideo()         
 sinavideo.download(url,""/Users/cheng/Documents/PyScript/res/"")
登入後複製

結果

Python爬蟲爬取影片的詳細介紹

以上是Python爬蟲爬取影片的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!