r=requests.get('http://httpbin.org/get').text print(r) # print(type(r)) # 如果要获取User-Agent的详细数值,需要做JSON转换 r_json=json.loads(r) print(r_json) print(type(r_json)) print(r_json['headers']['User-Agent'])
输出结果为:
1 "C:Program FilesAnaconda3python.exe" "D:/pycharm/test2/python gaoji.py" 2 { 3 "args": {}, 4 "headers": { 5 "Accept": "*/*", 6 "Accept-Encoding": "gzip, deflate", 7 "Connection": "close", 8 "Host": "httpbin.org", 9 "User-Agent": "python-requests/2.11.1" 10 }, 11 "origin": "114.252.235.73", 12 "url": "http://httpbin.org/get" 13 } 14 15 {'url': 'http://httpbin.org/get', 'headers': {'Connection': 'close', 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.11.1'}, 'origin': '114.252.235.73', 'args': {}} 16 <class 'dict'> 17 python-requests/2.11.1