如何存储来自网络请求响应的特定数据?
How to store specific data from web request response?
我正在执行这个发出网络请求的脚本:
#! /usr/bin/env python
import requests
import sched, time
import json
from pprint import pprint
cookies = {'_globalinstancekey': '1594227/1/7JtxqlA7M68O42U73B_MWA=='}
def main():
url = "http://transparency.hackxor.net/blog"
r = requests.get(url, cookies=cookies)
print "\nRequest: \n";
print vars(r)
if __name__ == '__main__':
main()
输出:
{'cookies': <RequestsCookieJar[]>, '_content': '<!DOCTYPE
html>\n<html>\n... comment from Black: removed, because irrelevant for
this post ...\n</html>', 'headers': {'X-Cache': 'HIT',
'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked', 'Age':
'56', 'Server': 'nginx', 'Connection': 'keep-alive', 'Cache-Control':
'public; max-age: 60;', 'Date': 'Sat, 05 Oct 2019 19:23:34 GMT',
'Content-Type': 'text/html'}, 'url':
u'http://transparency.hackxor.net/blog', 'status_code': 200,
'_content_consumed': True, 'encoding': 'ISO-8859-1', 'request':
<PreparedRequest [GET]>, 'connection': <requests.adapters.HTTPAdapter
object at 0x7f00a2bd7b90>, 'elapsed': datetime.timedelta(0, 0, 74170),
'raw': <urllib3.response.HTTPResponse object at 0x7f00a2c006d0>,
'reason': 'OK', '_next': None, 'history': []}
我试图将输出中的一些值保存到变量中,例如X-Cache
和 Age
.
的值
X-Cache
和 Age
在 headers 之下。要获取此信息,请执行以下操作:
>>> print r.headers['X-Cache']
HIT
>>> print r.headers['Age']
48
我正在执行这个发出网络请求的脚本:
#! /usr/bin/env python
import requests
import sched, time
import json
from pprint import pprint
cookies = {'_globalinstancekey': '1594227/1/7JtxqlA7M68O42U73B_MWA=='}
def main():
url = "http://transparency.hackxor.net/blog"
r = requests.get(url, cookies=cookies)
print "\nRequest: \n";
print vars(r)
if __name__ == '__main__':
main()
输出:
{'cookies': <RequestsCookieJar[]>, '_content': '<!DOCTYPE
html>\n<html>\n... comment from Black: removed, because irrelevant for
this post ...\n</html>', 'headers': {'X-Cache': 'HIT',
'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked', 'Age':
'56', 'Server': 'nginx', 'Connection': 'keep-alive', 'Cache-Control':
'public; max-age: 60;', 'Date': 'Sat, 05 Oct 2019 19:23:34 GMT',
'Content-Type': 'text/html'}, 'url':
u'http://transparency.hackxor.net/blog', 'status_code': 200,
'_content_consumed': True, 'encoding': 'ISO-8859-1', 'request':
<PreparedRequest [GET]>, 'connection': <requests.adapters.HTTPAdapter
object at 0x7f00a2bd7b90>, 'elapsed': datetime.timedelta(0, 0, 74170),
'raw': <urllib3.response.HTTPResponse object at 0x7f00a2c006d0>,
'reason': 'OK', '_next': None, 'history': []}
我试图将输出中的一些值保存到变量中,例如X-Cache
和 Age
.
X-Cache
和 Age
在 headers 之下。要获取此信息,请执行以下操作:
>>> print r.headers['X-Cache']
HIT
>>> print r.headers['Age']
48