Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to download a video from a webpage with python?

Tags:

python

video

I would like to pull the video from this website. http://www.jpopsuki.tv/video/Meisa-Kuroki---Bad-Girl/eec457785fba1b9bb35481f438cf35a7

I can access it with python and get the whole html. But the video's url is relative, i.e. looks like so: <source src="/images/media/eec457785fba1b9bb35481f438cf35a7_1351466328.mp4" type="video/mp4" />

Is there a way to pull it from the website using python?

like image 786
AVX Avatar asked Mar 07 '16 11:03

AVX


People also ask

Can I use Python to download files from website?

Requests is a versatile HTTP library in python with various applications. One of its applications is to download a file from web using the file URL.


1 Answers

Found the function below here

I think this'll do it:

import requests

def download_file(url):
    local_filename = url.split('/')[-1]
    # NOTE the stream=True parameter
    r = requests.get(url, stream=True)
    with open(local_filename, 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024): 
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)
                #f.flush() commented by recommendation from J.F.Sebastian
    return local_filename

download_file("http://www.jpopsuki.tv/images/media/eec457785fba1b9bb35481f438cf35a7_1351466328.mp4")
like image 140
jDo Avatar answered Oct 13 '22 00:10

jDo