How do I read a response from Python Requests?

前端 未结 3 772
天涯浪人
天涯浪人 2020-11-28 04:49

I have two Python scripts. One uses the Urllib2 library and one uses the Requests library.

I have found Requests easier to implement, but I can\'t find an equivalent

3条回答
  •  一整个雨季
    2020-11-28 05:38

    If the response is in json you could do something like (python3):

    import json
    import requests as reqs
    
    # Make the HTTP request.
    response = reqs.get('http://demo.ckan.org/api/3/action/group_list')
    
    # Use the json module to load CKAN's response into a dictionary.
    response_dict = json.loads(response.text)
    
    for i in response_dict:
        print("key: ", i, "val: ", response_dict[i])
    

    To see everything in the response you can use .__dict__:

    print(response.__dict__)
    

提交回复
热议问题