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

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

成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比遷安網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式遷安網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋遷安地區(qū)。費用合理售后完善,十余年實體公司更值得信賴。

Python主要用來做什么

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

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

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

一、爬蟲思路

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

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

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

二、爬取過程

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

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ā)現(xiàn)其在線播放網(wǎng)址是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,'集數(shù)'] = season_1_info[i].text
    Season_1.loc[i,'網(wǎng)址'] = 'http://www.imomoe.ai' + season_1_info[i].get('href')

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

第二步:獲取視頻地址

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,'網(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:當前的塊編號
    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中如何爬取并下載進擊的巨人全集視頻”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

新聞名稱:python中如何爬取并下載進擊的巨人全集視頻
文章地址:http://muchs.cn/article28/ihigcp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、手機網(wǎng)站建設、網(wǎng)站設計公司虛擬主機、品牌網(wǎng)站制作企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站