儿歌多多 下载全部儿歌视频文件

嗅探app的下载路径,然后抓取数据
 
http://30daydo.com/article/236 

更新 ******** 2018-01-09*************
最近才发现以前曾经挖了这个坑,现在来完成它吧。
 
儿歌多多在爱奇艺的视频网站上也有全集,所以目标转为抓取iqiyi的儿歌多多视频列表。 
github上有一个现成的下载iqiyi的第三方库,可以通过python调用这个库来实现下载功能。
 
代码使用python3来实现。
 
1. 打开网页 http://www.iqiyi.com/v_19rrkwcx6w.html#curid=455603300_26b870cbb10342a6b8a90f7f0b225685
 
这个是第一集的儿歌多多,url后面的curid只是一个随机的字符串,可以直接去掉。
 
url=“http://www.iqiyi.com/v_19rrkwcx6w.htm”
浏览器直接查看源码,里面直接包含了1-30集的视频url,那么要做的就是提取这30个url
 
先抓取网页内容:
session = requests.Session()
def getContent(url):
try:
ret = session.get(url)
except:
return None
if ret.status_code==200:
return ret.text
else:
return None

然后提取内容中的url

url.GIF

点击查看大图

提取div标签中的属性 data-current-count=1的。然后选择子节点中的li标签,提取li中的a中的href链接即可。
    content = getContent(url)
root = etree.HTML(content)
elements=root.xpath('//div[@data-current-count="1"]//li/a/@href')
for items in elements:
song_url = items.replace('//','')
song_url=song_url.strip()
print(song_url)







2. 有了url,就可以下载这个页面的中的内容了。
这里我使用的是一个第三方的视频下载库,you-get。(非常好用,fork到自己仓库研究 https://github.com/Rockyzsu/you-get)
 
使用方法:

python you-get -d --format=HD url
 
you-get 需要下载一个ffmpeg.exe的文件来解码视频流的,去官网就可以免费下载。
 
url就是你要下载的url地址,format可以选择高清还是其他格式的,我这里选择的是HD。
 
在python脚本里面调用另外一个python脚本,可以使用subprocess来调用。
        p=subprocess.Popen('python you-get -d --format=HD {}'.format(song_url),stderr=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
output,error = p.communicate()
print(output)
print(error)
p.wait()

上面的song_url 就是接着最上面那个代码的。
 
 
完整代码如下:
#-*-coding=utf-8-*-
import requests
from lxml import etree
import subprocess
session = requests.Session()
def getContent(url):
# url='http://www.iqiyi.com/v_19rrkwcx6w.html'
try:
ret = session.get(url)
# except Exception,e:
except:
# print e
return None
if ret.status_code==200:
return ret.text
else:
return None

def getUrl():
url='http://www.iqiyi.com/v_19rrkwcx6w.html'
url2='http://www.iqiyi.com/v_19rrl2td7g.html' # 31-61
content = getContent(url)
root = etree.HTML(content)
elements=root.xpath('//div[@data-current-count="1"]//li/a/@href')
for items in elements:
song_url = items.replace('//','')
song_url=song_url.strip()
print(song_url)
p=subprocess.Popen('python you-get -d --format=HD {}'.format(song_url),stderr=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
output,error = p.communicate()
print(output)
print(error)
p.wait()

def main():
getUrl()

if __name__ == '__main__':
main()

上面的是1-30集的,全部集数现在是120集,31-60的在url 
http://www.iqiyi.com/v_19rrl2td7g.html
 
这个url只要你选择iqiyi视频的右边栏,就会自动变化。 同理也可以找到61-120的url,然后替换到上面代码中最开始的url就可以了。(这里因为页码少,所以就没有用循环去做)
 
最终的下载结果:

ergeduoduo.GIF

 
 
后面修改了下代码,判断文件是不是已经存在,存在的会就不下载,对于长时间下载100多个文件很有用,如果掉线了,重新下载就不会去下载那些已经下好的。
#-*-coding=utf-8-*-
import sys,os
import requests
from lxml import etree
import subprocess
session = requests.Session()
def getContent(url):
# url='http://www.iqiyi.com/v_19rrkwcx6w.html'
try:
ret = requests.get(url)
ret.encoding='utf-8'
# except Exception,e:
except:
# print e
return None
if ret.status_code==200:
return ret.text
else:
return None

def getUrl():
url='http://www.iqiyi.com/v_19rrkwcx6w.html'
url2='http://www.iqiyi.com/v_19rrl2td7g.html' # 31-61
content = getContent(url)
if not content:
print "network issue, retry"
exit(0)
root = etree.HTML(content,parser=etree.HTMLParser(encoding='utf-8'))
elements=root.xpath('//div[@data-current-count="1"]//li')
for items in elements:
url_item=items.xpath('.//a/@href')[0]
song_url = url_item.replace('//','')
song_url=song_url.strip()
print(song_url)
# name=items.xpath('.//span[@class="item-num"]/text()')[0]
name=items.xpath('.//span[@class="item-num"]/text()')[0].encode('utf-8').strip()+\
' '+items.xpath('.//span[@class="item-txt"]/text()')[0].encode('utf-8').strip()+'.mp4'
name= '儿歌多多 '+name
name=name.decode('utf-8')
filename=os.path.join(os.getcwd(),name)
print filename
if os.path.exists(filename):
continue
p=subprocess.Popen('python you-get -d --format=HD {}'.format(song_url),stderr=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
output,error = p.communicate()
print(output)
print(error)
p.wait()


def main():
getUrl()

if __name__ == '__main__':
main()


下载下来大概300多集,直接拷贝到平板上离线播放,再也不卡了。
 
原创地址:http://30daydo.com/article/236 
转载请注明出处
 
 

0 个评论

要回复文章请先登录注册