python爬虫程序,下载梨视频到本地 环境搭建 关于python的介绍可以参考:python简介
python安装参考:Python安装以及版本检测
pycharm安装可以参考:PyCharm的安装和破解
pip安装参考:添加链接描述
request安装:pip install request
urllib安装:pip install urllib
需求分析 梨视频首页
具体实现 通过两次请求
源代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 from urllib.request import *from requests import *from re import *from os import *from random import *titlep = [] def single_page (url,name,video_num ): if path.exists(name): pass else : makedirs(name) response = get(url) response = response.text rep = findall('categoryem.*?href=\"video(.*?)\"' ,response,S) j = 0 for i in rep: j += 1 i = 'https://www.pearvideo.com/video' + i ori_video_url = one_video(i) print('正在下载第' ,j,'个视频...' ) suiji = random() full_name = name+'/' + titlep[0 ] + '.mp4' urlretrieve(ori_video_url, full_name) print('成功保存视频: ' ,full_name) if j == video_num: break ''' 当你的才华还撑不起你的野心时,那你就应该静下心来学习 当你的能力还驾驭不了你的目标时,那就应该沉下心来历练 ''' def one_video (url ): ''' 梨视频网站,传递一个单个视频页面,返回一个视频的原始地址 :param url: :return: ''' response = get(url) response = response.text rep = findall(r'https://video.pearvideo.com/mp4/(.*?)mp4' , response, S) titlee = findall(r'class="video-tt".*?>(.*?)</h1>' , response, S) for i in titlee: titlep.clear() titlep.append(i) print(titlep) for i in rep: i = 'https://video.pearvideo.com/mp4/' + i + 'mp4' print(i) return i def main (): mulu = { '新知' :'10' , '社会' :'1' , '世界' :'2' , '体育' :'9' , '生活' :'5' , '科技' :'8' , '娱乐' :'4' , '财富' :'3' , '汽车' :'31' , '美食' :'6' , '音乐' :'59' , } url = 'https://www.pearvideo.com/category_' for i in mulu: print(i,end=" " ) print() choice = input ("请选择需要的视频类型:" ) num = mulu[choice] url = url + num video_num = int (input ("请输入要下载视频的数量:" )) single_page(url,choice,video_num) main()