这是自己第一次做Python,以后坚持写10+的Python文章,一点点进步,一点点学习。现在是小白一枚多多见谅
#_*_ coding:utf-8 _*_
import re
import urllib.request
user_agent='Mozilla/4.0(compatible;MSIE 5.5;Windows NT)'
headers={'User-Agent':user_agent}
x=0
#循环爬取1到20页的视频
for i in range(1,20):
root = 'G:/PythonLearn/fileweek/' #储存路径
req_url_base='http://www.budejie.com/video/'+str(i) #爬取网页,注意i的规律,第一页为1,第二页为2
request=urllib.request.Request(url=req_url_base,headers=headers)
page=urllib.request.urlopen(request)
html=page.read().decode('utf-8')
reg=r'data-mp4="(.*?\.mp4)"'
imgre=re.compile(reg)
imglist=re.findall(imgre,html)
for imgurl in imglist:
urllib.request.urlretrieve(imgurl,root+'%s.mp4' %x)#储存视频到root的文件夹下面,视频名为x.MP4
x+=1
print("已爬取"+str(x))