1.作业一

  • 目标网站:https://www.pearvideo.com/
  • 爬取要求:
    • 1、用户输入视频页面的链接
    • 2、把所对应页面的视频,下载下来,保存到本地

代码:
from fake_useragent import UserAgent
import requests

url1=input(‘请输入你要爬取的视频链接’)
# 提取视频id
id=url1.split(‘_’)[1]
# 网站虚拟链接
url2=f”https://www.pearvideo.com/videoStatus.jsp?contId={id}
headers = {
‘User-Agent’ : UserAgent().chrome,
‘referer’: url1
}
# 请求虚拟链接
resp = requests.get(url2, headers=headers)
# 提取虚拟链接里的重要信息
uuu=resp.json()[‘videoInfo’][‘videos’][‘srcUrl’]
sp=str(uuu).rsplit(‘/‘,1)
# 拼成真实链接
url_machine = sp[0]+”/cont-“+id+”-“+sp[1].split(‘-‘,1)[1]

resp2 = requests.get(url_machine, headers=headers)

with open(‘1.mp4’,‘wb’) as f:
f.write(resp2.content)