13

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?

2
  • cant open the webpage, but have you tried FancyURLopener in the urllib library?
    – Deusdeorum
    Mar 7, 2016 at 11:51
  • @honorem it takes a bit of time but the page opens. I guess they have a very slow and old server. I haven't and have just looked up. Don't quite understand how I can fetch that relative link to the file.
    – AVX
    Mar 7, 2016 at 11:54

1 Answer 1

14

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")
5
  • What if there are multiple videos on the website, i.e. from ads? How do you specify which video? Feb 12, 2017 at 4:32
  • @Goldname My answer only explains how to download a video when you already have the URL. To download multiple videos from a website, you'd need to extract the links (may or may not be complicated depending on the website) and pass them to download_file one at a time.
    – jDo
    Feb 12, 2017 at 17:33
  • Are the links written in the html of the website? Sorry, I'm not sure what you mean by URL of the video. I thought the website that it was on was the URL of the video. Feb 12, 2017 at 17:35
  • 2
    @Goldname I used "URL" and "link" interchangeably although they're different things. "Are the links written in the html of the website?" That's impossible to say without knowing which website you’re referring to. You'll have to check out the HTML and try to figure out where/how the media files are stored. Once you've done that and obtained a bunch of direct links/URLs/addresses pointing to the files you're after, you can use the function in my answer (or curl, wget, whatever) to actually perform the download.
    – jDo
    Feb 12, 2017 at 17:48
  • @jDo is there a sure shot way to get the source link of the video from any website? I just want the links and not actually download the video. Apr 17, 2021 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.