Search code examples
pythonhtmlpython-requestsurllib

How to download image with a long URL length?


I am trying to download a picture from "https://prnt.sc", but the URL of the image is so long and I also can‘t find that URL when printing r.content.

This is my code for getting the HTML:

import requests
import random
import string
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) 
Chrome/76.0.3809.132 Safari/537.36 OPR/63.0.3368.71'
}


register_data = {"path" : "luk111"}
print (register_data)

with requests.Session() as s:
    url = 'https://prnt.sc/luk111'
    r = s.post(url, json=register_data, headers=headers)
    print(r)
    print (r.content)

The whole url has around 81954 characters, so I need a better way to download it. Any ideas?

This is my code for downloading the .jpg image:

import random
import urllib.request

def download_web_image(url):
    name = "xddd"
    full_name = "screen/" + str(name) + ".jpg"
    urllib.request.urlretrieve(url,full_name)

xd = input("paste url")

download_web_image(xd)

Solution

  • This long url on page is not real url but image's data in base64

    But first I turned off JavaScript in web browser and I checked this page without JavaScript because requests and BeautifulSoup can't run JavaScript.

    I see normal url to image and I have no problem to download it.

    import requests
    from bs4 import BeautifulSoup as BS
    
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 OPR/63.0.3368.71'
    }
    
    with requests.Session() as s:
        url = 'https://prnt.sc/luk111'
        r = s.get(url, headers=headers)
    
        soup = BS(r.content, 'html.parser')
        img_url = soup.find('img', {'id': 'screenshot-image'})['src']
    
        r = s.get(img_url, headers=headers)
        with open('temp.png', 'wb') as f:
            f.write(r.content)