2

So, I am requesting a URL that actually brings a picture, nothing else, you type the URL, click "enter" and it gets you a picture.

Now, I want to save that picture into a file in a desired path by using python but I am not succeding.

Can you help me?

Picture_request = requests.get(Photo_URL)

Where to go from there?

Roberto Bernal
  • 53
  • 1
  • 1
  • 4
  • 1
    What have you tried that didn't work ? (hint: how would you save your response's body to file if it was json, html or plain text ?) – bruno desthuilliers Jun 20 '18 at 12:26
  • Does this answer your question? [How to download image using requests](https://stackoverflow.com/questions/13137817/how-to-download-image-using-requests) – jdhao Jun 16 '20 at 04:11

1 Answers1

16

Try this:

import requests

picture_request = requests.get("https://example.com/picture/image.jpg")
if picture_request.status_code == 200:
    with open("image.jpg", 'wb') as f:
        f.write(picture_request.content)
KSs
  • 420
  • 3
  • 9
  • 2
    Thanks, I just solved it like minutes before reading your answer but yeah, thats exactly what I did. – Roberto Bernal Jun 20 '18 at 13:47
  • 1
    Posting comment on behalf of [@Burrito_007](https://stackoverflow.com/users/6088957) `import requests, os headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.3'} reg_url = 'https://mediacdn.99acres.com/8065/16/161316171M-1550756165.jpeg' test_path = os.path.join('/path/to/Desktop/img', 'cat.jpeg') Picture_request = requests.get(reg_url) if Picture_request.status_code == 200: with open(test_path, 'wb') as f: f.write(Picture_request.content)` This is not saving the image. – Dennis T --Reinstate Monica-- Mar 28 '19 at 14:05