Basic file uploading via website form using POST Requests in Python -
i try upload file on random website using python , http requests. this, use handy library named requests.
according the documentation, , answers on stackoverflow here , there, have add files
parameter in application, after studying dom of web page.
the method simple:
- look in source code url of form ("action" attribute);
- look in source code "name" attribute of uploading button ;
- look in source code "name" , "value" attributes of submit form button ;
- complete python code required parameters.
sometimes works fine. indeed, managed upload file on site : http://pastebin.ca/upload.php
after looking in source code, url of form upload.php
, buttons names file
, s
, value upload
, following code:
url = "http://pastebin.ca/upload.php" myfile = open("text.txt","rb") r = requests.get(url,data={'s':'upload'},files={'file':myfile}) print r.text.find("the uploaded file has been accepted.") # ≠ -1
but now, let's @ site: http://www.pictureshack.us/
the corresponding code follows:
url = "http://www.pictureshack.us/index2.php" myfile = open("text.txt","rb") r = requests.get(url,data={'upload':'upload picture'},files={'userfile':myfile}) print r.text.find("unsupported file type!") # = -1
in fact, difference see between these 2 sites first, url work done when submitting form same page form , files uploaded.
but not solve problem, because still not know how submit file in second case.
i tried make request on main page instead of .php, of course not work.
in addition, have question.
suppose form elements not have "name" attribute. how supposed designate @ request python?
for example, site: http://imagesup.org/
the submitting form button looks this: <input type="submit" value="héberger !">
how can use in data parameters?
the forms have component must honour: method
attribute. using get
requests, forms referring use method="post"
. use requests.post
send post
request.
Comments
Post a Comment