1.作业一
- 目标网站:https://www.pearvideo.com/
- 爬取要求:
- 1、用户输入视频页面的链接
- 2、把所对应页面的视频,下载下来,保存到本地
代码
# https://video.pearvideo.com/mp4/third/20220215/cont-1751883-10008579-112544-hd.mp4
# https://video.pearvideo.com/mp4/third/20220215/1645020658428-10008579-112544-hd.mp4
# “https://video.pearvideo.com/mp4/third/20220215/cont-1751874-15902642-093927-hd.mp4“
# https://www.pearvideo.com/video_1751883
import requests
urlvide = input(‘请输入视频链接:’)
# 提取 视频链接里的id
url_id = url_vide.split(‘‘)[1]
url = f’https://www.pearvideo.com/videoStatus.jsp?contId={url_id}&mrd=0.22348752870644506‘
headers = {
‘Referer’: url_id,
‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ‘
‘Chrome/95.0.4638.69 Safari/537.36 ‘
}
response = requests.get(url, headers=headers).json()
vide = response[‘videoInfo’][‘videos’][‘srcUrl’]
sp = str(vide).rsplit(‘/‘, 1)
# 拼接正确的url
url_videos = sp[0] + ‘/cont-‘+ url_id+’-‘+sp[1].split(‘-‘, 1)[1]
# 对拼接的url发请求
url_sp = requests.get(url=url_videos, headers=headers).content
with open(‘视频.mp4’,’wb’)as f:
f.write(url_sp)