1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        python爬蟲實戰(zhàn)之下載進擊的巨人全集視頻

        共 3546字,需瀏覽 8分鐘

         ·

        2021-10-03 17:23

        本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。

        爬取網(wǎng)站Url:http://www.imomoe.ai/


        一、爬蟲思路

        1、拿到所有集數(shù)和對應(yīng)的在線播放網(wǎng)址;

        2、從在線播放的網(wǎng)頁鏈接中找到視頻在服務(wù)器上的緩存地址;

        3、通過視頻地址將視頻下載到本地。

        二、爬取過程

        第一步:獲取所有集數(shù)和對應(yīng)的在線播放網(wǎng)址

        1、用BeautifulSoup匹配id屬性為play_0的div標(biāo)簽即可。代碼如下:

        season_1_url = 'http://www.imomoe.ai/view/4225.html'
        season_1_response = requests.get(season_1_url)
        season_1_response.encoding = 'gb2312'
        season_1_soup = BeautifulSoup(season_1_response.text, 'lxml')
        season_1_soup_info = season_1_soup.find('div', id="play_0")

        2、找到其中的a標(biāo)簽

        season_1_info = season_1_soup_info.find_all('a')

        3、點擊第一集,發(fā)現(xiàn)其在線播放網(wǎng)址是http://www.imomoe.ai/player/4225-0-0.html,其實就是http://www.imomoe.ai后面接上該標(biāo)簽下href屬性里的東西。編寫代碼如下:

        Season_1 = pd.DataFrame()
        for i in range(len(season_1_info)):
            Season_1.loc[i,'集數(shù)'] = season_1_info[i].text
            Season_1.loc[i,'網(wǎng)址'] = 'http://www.imomoe.ai' + season_1_info[i].get('href')

        獲取到了第一季每一集對應(yīng)的在線播放網(wǎng)址

        第二步:獲取視頻地址

        1、把iframe標(biāo)簽src屬性里的東西提取出來,再用正則表達式匹配視頻地址

        item = first_soup.find_all('iframe')[1].get('src')
        findLink = re.compile(r'vid=(.*?)&userlink=')
        re.findall(findLink,item)[0]

        2、循環(huán)獲取到第一季每一集的視頻地址

        findLink = re.compile(r'vid=(.*?)&userlink=')
        for i in range(len(Season_1)):
            url = Season_1.loc[i,'網(wǎng)址']
            driver = webdriver.Chrome()
            driver.get(url)
            response = driver.page_source
            soup = BeautifulSoup(response)
            item = soup.find_all('iframe')[1].get('src')
            Season_1.loc[i,'視頻地址'] = re.findall(findLink,item)[0]
            driver.quit()

        第三步:下載視頻

        用urllib.request.urlretrieve函數(shù)就能輕松下載

        path = r'C:\我的文件\迅雷下載\進擊的巨人'

        # 函數(shù)說明:回調(diào)函數(shù),打印下載進度
        def Progress(blocknum,blocksize,totalsize):
            
            """
            blocknum:當(dāng)前的塊編號
            blocksize:每次傳輸?shù)膲K大小
            totalsize:網(wǎng)頁文件總大小
            """
            percent = blocknum*blocksize/totalsize
            if percent > 1.0:
                percent = 1.0
            percent = percent*100
            print("\r%.2f%% 已下載:%.2f Mb 文件大?。?.2f Mb" %(percent,blocknum*blocksize/1e6,totalsize/1e6), end='     ')

        for i in range(len(Season_1)):
            download_url = Season_1.loc[i,'視頻地址']
            if os.path.exists(path + './第一季') != 1:
                os.mkdir(path + './第一季')
            
            filename = os.path.join(path, '第一季', Season_1.loc[i,'集數(shù)']+'.mp4')
            
            print('正在下載%s' %Season_1.loc[i,'集數(shù)'])
            urllib.request.urlretrieve(download_url, filename, Progress)
            print()

        以上就是python爬蟲中爬取下載進擊的巨人全集視頻的思路和具體下載過程,想要下載的小伙伴可以按照小編這個步驟一步步進行哦~

        搜索下方加老師微信

        老師微信號:XTUOL1988切記備注學(xué)習(xí)Python

        領(lǐng)取Python web開發(fā),Python爬蟲,Python數(shù)據(jù)分析,人工智能等精品學(xué)習(xí)課程。帶你從零基礎(chǔ)系統(tǒng)性的學(xué)好Python!

        *聲明:本文于網(wǎng)絡(luò)整理,版權(quán)歸原作者所有,如來源信息有誤或侵犯權(quán)益,請聯(lián)系我們刪除或授權(quán)


        瀏覽 58
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            91探花国产综合在线精品最新章节 | 俄罗斯2一3sex性hd | 欧美粗大视频 | 大香蕉伊人免费在线观看 | 初尝禁果稚嫩宫交h疯狂的兔 | 九九九九国产 | 中文字幕高清无码视频 | jizzyou中国少妇东北 | 天天干天天舔 | 欧美性爱综合 |