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

本文教程操作環(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、通過視頻地址將視頻下載到本地。
二、爬取過程
第一步:獲取所有集數(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屬性里的東西提取出來,再用正則表達(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ù)說明:回調(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()
以上就是python爬蟲中爬取下載進(jìn)擊的巨人全集視頻的思路和具體下載過程,想要下載的小伙伴可以按照小編這個(gè)步驟一步步進(jìn)行哦~
搜索下方加老師微信
老師微信號(hào):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)益,請(qǐng)聯(lián)系我們刪除或授權(quán)


