#下载网页中的视频
import urllib.request
import re#正则表达式
import os
#找到起始网页
url ='https://www.pearvideo.com/category_8'
html = urllib.request.urlopen(url).read().decode()#源代码
#获取ID
video_id = re.findall('<a href="(.*?)" class="vervideo-lilink actplay">',html)#找到所需数据然后copy,进行定位,这是一个原网址,其他视频的网址是在其基础上改变的
name =re.findall('<div class="vervideo-title">(.*?)</div>',html)
video_url =[]
for id in video_id:
newurl ='https://www.pearvideo.com/%s' % id#拼接网址
video_url.append(newurl)#将newurl加入列表
address =[]
for i in video_url:
response = urllib.request.urlopen(i).read().decode()
address1 = re.findall('hdUrl="",sdUrl="",ldUrl="",srcUrl="(.*?)",vdoUrl=srcUrl',response)[0]
address.append(address1)#找到视频的播放地址
#下载视频,将名字与地址写成字典
success =dict(zip(name,address))
for j ,ad in success.items():
urllib.request.urlretrieve(ad,'C:\\Users\\lenovo\Desktop\\{}.mp4'.format(j))#用urlretrieve下载图片,format处理图片保存名字