溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python中如何爬取并下載進擊的巨人全集視頻

發(fā)布時間:2021-05-08 09:27:50 來源:億速云 閱讀:93 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關python中如何爬取并下載進擊的巨人全集視頻的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

Python主要用來做什么

Python主要應用于:1、Web開發(fā);2、數據科學研究;3、網絡爬蟲;4、嵌入式應用開發(fā);5、游戲開發(fā);6、桌面應用開發(fā)。

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

爬取網站Url:http://www.imomoe.ai/

一、爬蟲思路

1、拿到所有集數和對應的在線播放網址;

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

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

二、爬取過程

第一步:獲取所有集數和對應的在線播放網址

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

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標簽

season_1_info = season_1_soup_info.find_all('a')

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

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

獲取到了第一季每一集對應的在線播放網址

第二步:獲取視頻地址

1、把iframe標簽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,'網址']
    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函數就能輕松下載

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

# 函數說明:回調函數,打印下載進度
def Progress(blocknum,blocksize,totalsize):
    
    """
    blocknum:當前的塊編號
    blocksize:每次傳輸的塊大小
    totalsize:網頁文件總大小
    """
    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,'集數']+'.mp4')
    
    print('正在下載%s' %Season_1.loc[i,'集數'])
    urllib.request.urlretrieve(download_url, filename, Progress)
    print()

感謝各位的閱讀!關于“python中如何爬取并下載進擊的巨人全集視頻”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI