這篇文章給大家分享的是有關(guān)python中如何爬取并下載進(jìn)擊的巨人全集視頻的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比遷安網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式遷安網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋遷安地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。
Python主要應(yīng)用于:1、Web開發(fā);2、數(shù)據(jù)科學(xué)研究;3、網(wǎng)絡(luò)爬蟲;4、嵌入式應(yīng)用開發(fā);5、游戲開發(fā);6、桌面應(yīng)用開發(fā)。
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
爬取網(wǎng)站Url:http://www.imomoe.ai/
一、爬蟲思路
1、拿到所有集數(shù)和對(duì)應(yīng)的在線播放網(wǎng)址;
2、從在線播放的網(wǎng)頁(yè)鏈接中找到視頻在服務(wù)器上的緩存地址;
3、通過(guò)視頻地址將視頻下載到本地。
二、爬取過(guò)程
第一步:獲取所有集數(shù)和對(duì)應(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、點(diǎn)擊第一集,發(fā)現(xiàn)其在線播放網(wǎng)址是http://www.imomoe.ai/player/4225-0-0.html,其實(shí)就是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')
獲取到了第一季每一集對(duì)應(yīng)的在線播放網(wǎng)址
第二步:獲取視頻地址
1、把iframe標(biāo)簽src屬性里的東西提取出來(lái),再用正則表達(dá)式匹配視頻地址
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:\我的文件\迅雷下載\進(jìn)擊的巨人' # 函數(shù)說(shuō)明:回調(diào)函數(shù),打印下載進(jìn)度 def Progress(blocknum,blocksize,totalsize): """ blocknum:當(dāng)前的塊編號(hào) blocksize:每次傳輸?shù)膲K大小 totalsize:網(wǎng)頁(yè)文件總大小 """ 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()
感謝各位的閱讀!關(guān)于“python中如何爬取并下載進(jìn)擊的巨人全集視頻”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
當(dāng)前文章:python中如何爬取并下載進(jìn)擊的巨人全集視頻
網(wǎng)站鏈接:http://chinadenli.net/article28/ihigcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、自適應(yīng)網(wǎng)站、關(guān)鍵詞優(yōu)化、網(wǎng)站內(nèi)鏈、ChatGPT、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)