0

so, i've seen many different posts around here but nothing answered to all of my problems.. i am trying to download an image from a website, but in order to do so, I have to login to it. I have the Username (email) and password, and the image link. problem is, i tried 4-5 different scripts and none worked.. some used urllib which couldn't log in (resulted with the image saying "You are not logged in"), some used Requests which.. i actually don't know if it even saved the image because the image I recieved was corrupted or.. i dont know.. basically the image i was supposed to recieve is 41.2 kb and the corrupt image i recieved was 12 kb.. so thats that.. what i'm currently using, i deleted the email and password and changed the links..

from requests import Session
import shutil
import urllib
block=874
page=1
url1='http://website.com/images/blocks/%s/%s.png'%(str(block),str(page))
url = 'https://website.com/login.php'
s = Session()
s.post(url, {"password":"12345678", "email":"derpty@derp.com", "status":""})
res=s.get(url1,stream=True)
with open('img.png','wb') as out_file:
    res.raw.decode_content = True
    out_file.write(res.content)
del res

now I'm really new to this thing.. I don't even know if all i posted here is supposed to work or not.. well I guess that's why i'm posting here.. Thanks in advance!

user3203956
  • 13
  • 1
  • 4
  • That looks like someone picked [parts of my solution on downloading images](https://stackoverflow.com/questions/13137817/how-to-download-image-using-requests/13137873#13137873). The `res.raw.decode_content = True` is useless when using `res.content` on the next line. You may as well remove the `stream=True` in that case too. – Martijn Pieters Mar 31 '15 at 08:50
  • In other words, either implement the *whole* streaming approach and use `shutil.copyfileobj(res.raw, out_file)` or don't use streaming at all. – Martijn Pieters Mar 31 '15 at 08:54
  • umm.. Ill.. try whatever you tell me to try.. how do I do that? EDIT: I added the line right after the `out_file.write` and now I got a 2KB corrupt image...? – user3203956 Mar 31 '15 at 09:03
  • It sounds like you don't fully understand the process here. You need to make 2 separate HTTP[s] requests. The first one is the login request. Part of the response to that request will be some an authentication token (AKA your login cookie). The second request you make (to actually download the image) will need to contain the same authentication token in some form (probably sent in the request header). You could use your browser's developer tools to understand how you get the authentication token from the login response, and how it's submitted back to the website, before you try to code it. – Tom Dalton Mar 31 '15 at 09:17
  • Any further information on where/how..? I sort of understand what you are trying to tell me but I don't know how to do it – user3203956 Mar 31 '15 at 09:27
  • @user3203956: did you read the linked answer and understand the differences between what I posted there and what you are using here? – Martijn Pieters Mar 31 '15 at 13:03
  • @MartijnPieters I tired.. again I'm unfamiliar with the subject so i didn't quite understand it.. – user3203956 Mar 31 '15 at 17:38

0 Answers0