如何获取字符串中某个具体的数值--通过json.load转化成字典形式获取

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 Files\Anaconda3\python.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

 

posted @ 2019-01-18 11:49  Smilevv-45  阅读(551)  评论(0编辑  收藏  举报