How to send a “multipart/form-data” with requests in python?

前端 未结 9 1736
野趣味
野趣味 2020-11-22 01:29

How to send a multipart/form-data with requests in python? How to send a file, I understand, but how to send the form data by this method can not understand.

9条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 01:41

    You need to use the name attribute of the upload file that is in the HTML of the site. Example:

    autocomplete="off" name="image">
    

    You see name="image">? You can find it in the HTML of a site for uploading the file. You need to use it to upload the file with Multipart/form-data

    script:

    import requests
    
    site = 'https://prnt.sc/upload.php' # the site where you upload the file
    filename = 'image.jpg'  # name example
    

    Here, in the place of image, add the name of the upload file in HTML

    up = {'image':(filename, open(filename, 'rb'), "multipart/form-data")}
    

    If the upload requires to click the button for upload, you can use like that:

    data = {
         "Button" : "Submit",
    }
    

    Then start the request

    request = requests.post(site, files=up, data=data)
    

    And done, file uploaded succesfully

提交回复
热议问题