python爬虫实战之下载进击的巨人全集视频

python爬虫实战之下载进击的巨人全集视频,第1张

python爬虫实战之下载进击的巨人全集视频

python爬虫可以通过模拟浏览器打开网页,获取网页中我们想要的那部分数据,利用爬虫我们可以获取大量的价值数据例如最近封禁的动漫进击的巨人。我们可以通过python爬虫爬取下载全集内容。

爬取网站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、点击第一集,发现其在线播放网址是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、循环获取到第一季每一集的视频地址

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爬虫中爬取下载进击的巨人全集视频的思路和具体下载过程,想要下载的小伙伴可以按照小编这个步骤一步步进行哦~更多python爬虫知识:python爬虫。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/3015019.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-28
下一篇 2022-09-28

发表评论

登录后才能评论

评论列表(0条)

保存