1

Hi I want to download an image in this link, but I can't open the created image. This is the output file enter image description here How can I solve this? This is the code:

from requests.models import Response
import requests

url = 'https://beta.mangaeden.com/it/it-manga/jojo-no-kimyou-na-bouken---jojorion/1/1'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
immagine = soup.find(id='mainImg')
src = str(immagine.get('src'))
link_immagine = "https:"+src
print(link_immagine)
'''download imagege'''
response = requests.get(link_immagine)
f = open("C:\\Users\\chris\\Desktop\\image.jpg", "wb")
f.write(response.content)
f.close()
print('download successfull')
Hamza Anis
  • 2,475
  • 1
  • 26
  • 36
  • please see this: https://stackoverflow.com/a/8286449/11754210 – Samu Nemeth Aug 22 '21 at 19:49
  • 1
    Does this answer your question? [How to download image using requests](https://stackoverflow.com/questions/13137817/how-to-download-image-using-requests) – RJ Adriaansen Aug 22 '21 at 19:51
  • This is not a problem with downloading the image but with the access of the image. There is a 403 response on making the request. – Hamza Anis Aug 22 '21 at 20:05

2 Answers2

0

That website seems to return 403 Forbidden errors based on your user agent. It should work if you request the image using a browser's user agent, for example:

response = requests.get(link_immagine, headers={
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0 Safari/537.36'
})
altermetax
  • 696
  • 7
  • 16
0

The problem while downloading this image is actually with getting the request as the response return the image link is 403 but if you set the User-Agent in the headers, it will do the trick and you get 200 after that you can save the image.

from requests.models import Response
import requests
from bs4 import BeautifulSoup

url = 'https://beta.mangaeden.com/it/it-manga/jojo-no-kimyou-na-bouken---jojorion/1/1'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
immagine = soup.find(id='mainImg')
src = str(immagine.get('src'))
link_image = "https:"+src
print(link_image)
'''download link_image'''
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Safari/605.1.15'}
response = requests.get(link_image,headers=headers)

if response.status_code == 200:
    with open("download.jpg", 'wb') as f:
        f.write(response.content)
else:
  print("NOT FOUND")

The image that you were saving earlier was just a response from 403 Forbidden and was not the actual image data.

Hamza Anis
  • 2,475
  • 1
  • 26
  • 36