Python : Trying to POST form using requests

后端 未结 3 1330
忘掉有多难
忘掉有多难 2020-11-29 03:37

I\'m trying to login a website for some scraping using Python and requests library, I am trying the following (which doesn\'t work):

import requests
headers          


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 04:09

    I was having problems here (i.e. sending form-data whilst uploading a file) until I used the following:

    files = {'file': (filename, open(filepath, 'rb'), 'text/xml'),
             'Content-Disposition': 'form-data; name="file"; filename="' + filename + '"',
             'Content-Type': 'text/xml'}
    

    That's the input that ended up working for me. In Chrome Dev Tools -> Network tab, I clicked the request I was interested in. In the Headers tab, there's a Form Data section, and it showed both the Content-Disposition and the Content-Type headers being set there.

    I did NOT need to set headers in the actual requests.post() command for this to succeed (including them actually caused it to fail)

提交回复
热议问题