如何使用Python Selenium下载Reddit视频

时间:2019-01-23 14:16:00

标签: python python-3.x selenium selenium-webdriver

我可以通过在开发人员工具中转到网络面板来下载视频。但是我该如何使用python +硒呢?

1 个答案:

答案 0 :(得分:0)

您需要获取文件托管的URL,并使用requests下载并将其写入文件。像这样

import requests

... <selnium code to parse web page>

def save_vid(name=None, url=None):
    if not name:
        name = 'video.mp4'

    user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'
    response  = requests.get(url, headers={'User-Agent': user_agent})

    with open(name, 'wb') as f:
        print ("Donloading chunck")
        for chunk in response.iter_content(chunk_size=255): 
            if chunk:
                f.write(chunk)
    print("Done")

更新:

专门针对reddit,获取了视频的来源

<video poster="https://external-preview.redd.it/lrJKPYDzKs2SYt2rc0ig4hipMuD4JNPn24fjrV3NMLQ.png?width=960&amp;crop=smart&amp;format=pjpg&amp;auto=webp&amp;s=44e72857f61ea62d7a447a9246961aeb3ae0322d" muted="" preload="auto" class="HTML5StreamPlayer__video__regular" src="blob:https://www.reddit.com/dbfa2e9f-07da-4bc3-b258-d12ba9c4ca67">
 --> <source src="https://v.redd.it/l67upl0tug921/HLSPlaylist.m3u8" type="application/vnd.apple.mpegURL">
</video>

获取视频ID

来自src="https://v.redd.it/l67upl0tug921/HLSPlaylist.m3u8, ID = l67upl0tug921

托管视频https://v.redd.it/l67upl0tug921/DASH_4_8_M

使用上面的功能下载。

  

注意:请确保在请求中使用用户代理+僵尸程序延迟1秒(根据reddit)