0

I want make a program to download jpg files (japanese comics) from some urls, I saw some examples around the web but they don't work in my case:

import urllib2
jpgfile = urllib2.urlopen("http://mangas2013.centraldemangas.com.br/attack_on_titan/attack_on_titan001-01.jpg")
output = open('attack_on_titan001-01.jpg','wb')
output.write(jpgfile.read())
output.close()

With this url I get a 28kb jpg file file (the original is 120kb), and when I try to open, the image doesn't appear on windows picture viewer... It's odd because I can download and view jpg files from other sites using the same code...

I'm a newbie in python, so try to give me the most simple answers possible.

Tom Zych
  • 13,329
  • 9
  • 36
  • 53

1 Answers1

0

You might try using urllib.urlretrieve() instead of urllib2.urlopen.

import urllib
jpg_filename, headers = urllib.urlretrieve('http://mangas2013.centraldemangas.com.br/attack_on_titan/attack_on_titan001-01.jpg', 'attack_on_titan001-01.jpg')

edit: I re-read your question, and I'm not sure exactly why that site in particular wouldn't work. It's probably because you need to be authenticated before you can access that file. Check what response you're getting:

import urllib2
jpgfile = urllib2.urlopen("http://mangas2013.centraldemangas.com.br/attack_on_titan/attack_on_titan001-01.jpg")
print jpgfile.getcode()
print jpgfile.read()

It's likely a redirect because of lack of authentication.

  • I got the same error, did you try to run your code? – Yago Lima Feb 20 '14 at 09:28
  • You very likely need to be authenticated on that site in order to access the jpeg file. You could use a proxy server and your browser to look at valid requests to see what that site uses for authentication, but I'm sure you're violating some Terms of Service. It likely requires the right cookie values to be set. Check out http://stackoverflow.com/a/8206372/2337592 – David Ginsburg Feb 20 '14 at 09:49
  • I got a long html code as response, the code is from the comic's main page: http://centraldemangas.com.br/mangas/info/attack-on-titan – Yago Lima Feb 20 '14 at 09:51