Saving response from Requests to file

前端 未结 2 1954
你的背包
你的背包 2020-12-16 09:46

I\'m using Requests to upload a PDF to an API. It is stored as \"response\" below. I\'m trying to write that out to Excel.

import requests

files = {\'f\': (         


        
相关标签:
2条回答
  • As Peter already pointed out:

    In [1]: import requests
    
    In [2]: r = requests.get('https://api.github.com/events')
    
    In [3]: type(r)
    Out[3]: requests.models.Response
    
    In [4]: type(r.content)
    Out[4]: str
    

    You may also want to check r.text.

    Also: https://2.python-requests.org/en/latest/user/quickstart/

    0 讨论(0)
  • 2020-12-16 10:23

    You can use the response.text to write to a file:

    import requests
    
    files = {'f': ('1.pdf', open('1.pdf', 'rb'))}
    response = requests.post("https://pdftables.com/api?&format=xlsx-single",files=files)
    response.raise_for_status() # ensure we notice bad responses
    file = open("resp_text.txt", "w")
    file.write(response.text)
    file.close()
    file = open("resp_content.txt", "w")
    file.write(response.text)
    file.close()
    
    0 讨论(0)
提交回复
热议问题