使用requests库爬取并下载梨视频

url分析

以梨视频 https://www.pearvideo.com/video_1728019 为例,检查元素可以找到video链接,页面源代码没有,在Network中筛选XHR可找到

inspect element链接(有效):https://video.pearvideo.com/mp4/adshort/20210427/cont-1728019-15665358_adpkg-ad_hd.mp4

Network srcUrl链接(404):https://video.pearvideo.com/mp4/adshort/20210427/1629376413683-15665358_adpkg-ad_hd.mp4

只有加粗部分有区别,1728019为视频id,1629376413683为systemTime,获取srcUrl后replace即可得到视频下载链接。

headers找到url:https://www.pearvideo.com/videoStatus.jsp?contId=1728019&mrd=0.6482209940899077

使用requests库爬取并下载梨视频

使用requests库爬取并下载梨视频

此时需要进行防盗链处理,找到referer(本次请求的上一级url):

使用requests库爬取并下载梨视频

代码

import requests

# 1. 网页链接
url = "https://www.pearvideo.com/video_1728019"
# 2. 视频id
contId = url.split('_')[1]

# 3. ua,防盗链
headers_dict = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                  "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36",
    # 防盗链:溯源(referer)
    "referer": url
}

# 4. 网页视频链接
videoStatusUrl = f"https://www.pearvideo.com/videoStatus.jsp?contId={contId}&mrd=0.6783504423465614"
response = requests.get(videoStatusUrl, headers=headers_dict)
# 5. 获取response
result = response.json()
response.close()
# 6. 获取视频源链接
src_url = result['videoInfo']['videos']['srcUrl']
systemTime = result['systemTime']
# https://video.pearvideo.com/mp4/adshort/20210427/cont-1728019-15665358_adpkg-ad_hd.mp4
# https://video.pearvideo.com/mp4/adshort/20210427/1629376413683-15665358_adpkg-ad_hd.mp4
videoUrl = src_url.replace(systemTime, f"cont-{contId}")
print(videoUrl)

# 7. 下载视频
with open(f"pearvideo_{contId}.mp4", mode="wb") as f:
    f.write(requests.get(videoUrl).content)
上一篇:MP4(AVI)视频转jpg(png)图片


下一篇:C++使用mediainfo