Python : Trying to POST form using requests

后端 未结 3 1302
忘掉有多难
忘掉有多难 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 03:45

    Send a POST request with content type = 'form-data':

    import requests
    files = {
        'username': (None, 'myusername'),
        'password': (None, 'mypassword'),
    }
    response = requests.post('https://example.com/abc', files=files)
    
    0 讨论(0)
  • 2020-11-29 03:53

    You can use the Session object

    import requests
    headers = {'User-Agent': 'Mozilla/5.0'}
    payload = {'username':'niceusername','password':'123456'}
    
    session = requests.Session()
    session.post('https://admin.example.com/login.php',headers=headers,data=payload)
    # the session instance holds the cookie. So use it to get/post later.
    # e.g. session.get('https://example.com/profile')
    
    0 讨论(0)
  • 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)

    0 讨论(0)
提交回复
热议问题