how to upload python files?

Asked

Viewed 871 times

1

what method of the request or urllib.request library do I use to upload files? (Example: upload photos to the drive automatically,or to social networks automatically or even upload videos to youtube)

  • Your question is very wide. But here’s a hint: Look up the API documentation. Youtube has an API for uploading video, such as Google Drive, Dropbox, Onedrive etc.

1 answer

0

Normally you would use the requests.post - however, that’s the easy part.

The point is that these pages in general do not simply have an HTML form, with a field of the type <input type="file" ...> - if that were the case, it would only be a matter of passing the parameter files= in the call to requests.post - and requests itself takes care of setting http headers correctly, (Sending files requires content-type to be multipart/form-data, for example).

.

So, if you have a page that is the destination of an HTTP form, and that has one or more fields input to receive files, just make a call like:

requests.post("http://pagina.de.envio.com", files={"foto": open("meuarquivo.png", "rb")})

(in that case, assuming that the name country input of the form be the word foto). You can send more information about the file, such as its name, mimetype, etc - and send other data from the same form by passing the parameter data= on the call of post, as usual. The documentation (which unfortunately has only a few examples, not the exact description) is in: http://docs.python-requests.org/en/master/user/quickstart/#post-a-Multipart-encoded-file

Now - even for a simple site with a "web 1.0" form, you have to be logged in, in general - this requires you to use the requests library to create a session, sending in previous posts the login and password, and then you send the post from that session, not direct the requests.post

the bad news

is that the above method only works for simple pages, in which you have a direct form for image upload. The sites you mentioned - google-drive and social networks, use much more complex mechanisms to upload images, and you’ll hardly get anything by sending a post as if it were a filled form, which actually emulates what a browser does when it sends a form. The Web interface of uploading these sites uses a portion of custom code (javascript, front-end) and in general will make very specific requests to send the data of the file itself.

Reverse-engineering how a file is sent by the interface on these sites would be a giant task. On the other hand, several of these sites - google-drive specifically feature an API for programmatic interaction. That is, you will not use the requests Python to interact with the site as if it were a browser looking at HTML content - and yes, it will call site-specific addresses, with well-documented content, usually encoded as JSON to do tasks like upload files or other interactions. (In such cases, instead of calling the requests.post with the parameter file=, you will call a specific method to upload images or files from the API library)

In general, websites that offer an API also offer, in addition to documentation, a Python package that you will use - instead of requests - to interact with the site. And continuing the bad news: in order to be able to use the API, and use this API library, every such site works one way - you’re going to have to, for every site, study the documentation, learn how to generate a key, install the API library, and learn how to use it -

For each site start by doing a search like: "google drive api file upload tutorial", "facebook api photo upload tutorial" - and be prepared for a long but exciting journey.

For example, for google drive, after installing the API library, setting up an access key for your app, and making the required Imports, the code to upload a file is:

file_metadata = {'name': 'photo.jpg'}
media = MediaFileUpload('files/photo.jpg',
                        mimetype='image/jpeg')
file = drive_service.files().create(body=file_metadata,
                                    media_body=media,
                                    fields='id').execute()
print ('File ID: %s' % file.get('id'))

(example taken from https://developers.google.com/drive/api/v3/manage-uploads#simple , API setup documentation for Python is on: https://developers.google.com/drive/api/v3/quickstart/python )

The code is even simple - but the documentation of the step by step can be complicated. And this is for each site.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.