12

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?

AVX
  • 319
  • 1
  • 3
  • 13
  • cant open the webpage, but have you tried [FancyURLopener](https://docs.python.org/2/library/urllib.html#urllib.FancyURLopener) in the urllib library? – Deusdeorum Mar 07 '16 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 07 '16 at 11:54

1 Answers1

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")
jDo
  • 3,962
  • 1
  • 11
  • 30
  • What if there are multiple videos on the website, i.e. from ads? How do you specify which video? – JobHunter69 Feb 12 '17 at 04: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 '17 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. – JobHunter69 Feb 12 '17 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 '17 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. – Kashish Arora Apr 17 '21 at 07:27