温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python中如何爬取并下载进击的巨人全集视频

发布时间:2021-05-08 09:27:50 来源:亿速云 阅读:92 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关python中如何爬取并下载进击的巨人全集视频的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

Python主要用来做什么

Python主要应用于:1、Web开发;2、数据科学研究;3、网络爬虫;4、嵌入式应用开发;5、游戏开发;6、桌面应用开发。

本文教程操作环境:windows7系统、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、点击第一集,发现其在线播放网址是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中如何爬取并下载进击的巨人全集视频”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI