我希望访问响应的timing
部分的值。你知道吗
我有以下JSON响应。你知道吗
{
"method": "Network.responseReceived",
"params": {
"frameId": "(297304CB88DA6BF7ED17760594B93F3E)",
"loaderId": "9976.1",
"requestId": "9976.6",
"response": {
"connectionId": 84,
"connectionReused": false,
"encodedDataLength": 453,
"fromDiskCache": false,
"fromServiceWorker": false,
"headers": {
"Cache-Control": "max-age=2592000",
"Connection": "keep-alive",
"Content-Encoding": "gzip",
"Content-Type": "text/css",
"Date": "Thu, 25 Jan 2018 19:47:50 GMT",
"Expires": "Sat, 24 Feb 2018 19:47:50 GMT",
"Last-Modified": "Fri, 20 Oct 2017 22:53:18 GMT",
"Pragma": "public",
"Server": "nginx",
"Transfer-Encoding": "chunked",
"Vary": "Accept-Encoding",
"X-Content-Type-Options": "nosniff",
"X-Nginx-Cache-Status": "HIT",
"X-Server-Powered-By": "Engintron",
"X-XSS-Protection": "1; mode=block"
},
"headersText": "HTTP/1.1 200 OK\r\nServer: nginx\r\nDate: Thu, 25 Jan 2018 19:47:50 GMT\r\nContent-Type: text/css\r\nTransfer-Encoding: chunked\r\nConnection: keep-alive\r\nVary: Accept-Encoding\r\nLast-Modified: Fri, 20 Oct 2017 22:53:18 GMT\r\nExpires: Sat, 24 Feb 2018 19:47:50 GMT\r\nCache-Control: max-age=2592000\r\nX-XSS-Protection: 1; mode=block\r\nX-Content-Type-Options: nosniff\r\nX-Nginx-Cache-Status: HIT\r\nX-Server-Powered-By: Engintron\r\nPragma: public\r\nContent-Encoding: gzip\r\n\r\n",
"mimeType": "text/css",
"protocol": "http/1.1",
"remoteIPAddress": "213.251.187.58",
"remotePort": 80,
"requestHeaders": {
"Accept": "text/css,*/*;q=0.1",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en;q=0.9",
"Connection": "keep-alive",
"Host": "www.oostcircle.nl",
"Referer": "http://example",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
},
"requestHeadersText": "GET /static/personal/css/style.min.css HTTP/1.1\r\nHost: example.com\r\nConnection: keep-alive\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36\r\nAccept: text/css,*/*;q=0.1\r\nReferer: http://example.com/\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-US,en;q=0.9\r\n",
"securityState": "neutral",
"status": 200,
"statusText": "OK",
"timing": {
"connectEnd": 25.7599998731166,
"connectStart": 13.7589999940246,
"dnsEnd": 13.7589999940246,
"dnsStart": 3.55999986641109,
"proxyEnd": -1,
"proxyStart": -1,
"pushEnd": 0,
"pushStart": 0,
"receiveHeadersEnd": 45.4459998290986,
"requestTime": 1296269.240561,
"sendEnd": 26.484000030905,
"sendStart": 26.1399999726564,
"sslEnd": -1,
"sslStart": -1,
"workerReady": -1,
"workerStart": -1
},
"url": "http://www.example.com/static/personal/css/style.min.css"
},
"timestamp": 1296269.289171,
"type": "Stylesheet"
}
},
我已经成功地提取了type
和url
值,但是当我尝试使用以下方法提取计时时:
with open(file_) as json_file:
data = json.load(json_file, parse_int=Decimal, parse_float=Decimal)
for p in data:
if "Network.responseReceived" in p['method']:
print('URL: ' + p['params']['response']['timing']['pushEnd'])
使用Python文档中指定的十进制解析器https://docs.python.org/3/library/json.html
我得到一个错误:
TypeError: must be str, not decimal.Decimal
如果没有解析器,我得到:
TypeError: must be str, not int
我试过使用[str('pushEnd')]
,但得到了相同的错误。你知道吗
如何从JSON访问和返回整数和浮点
完全回溯是:
Traceback (most recent call last):
File "e:/calc_time.py", line 17, in <module>
print('URL: ' + p['params']['response']['timing'][str('pushEnd')])
TypeError: must be str, not int
正如我提到的,我完全不明白为什么要将int和float解析为小数。但不管怎样,这根本不是问题的原因,因为无论有无错误,都会出现错误。你知道吗
错误回溯准确地显示了错误所在:您试图将字符串与int或decimal连接起来。Python不允许这样做;只能将字符串与其他字符串连接起来。你知道吗
有多种方法可以解决这个问题,但最好的方法是不要连接。使用字符串插值代替。你知道吗
(请注意,您试图通过执行
p...[str('pushEnd')]
来解决这个问题,这根本不是正确的做法;这不是关键,“pushEnd”,这就是问题所在,无论如何,这已经是一个字符串;这是一个值,即该查找的结果,这是一个int。因此您可以执行str(p....['pushEnd'])
,但正如我所说的,使用插值更好。)相关问题 更多 >
编程相关推荐