解析Python response.text

解析Python response.text,python,parsing,Python,Parsing,我是Python新手,需要一些帮助 我有一个剧本: import requests url = "https://www.example.com/vtapi/v2/ip-address/report?apikey=XXXXXXXXXXXXXXX&ip=8.8.8.8" r = requests.get(url, verify=False) print(r.text) 书面答复如下: {"asn": 45899, "undetected_

我是Python新手,需要一些帮助

我有一个剧本:

import requests
url = "https://www.example.com/vtapi/v2/ip-address/report?apikey=XXXXXXXXXXXXXXX&ip=8.8.8.8"
r = requests.get(url, verify=False)
print(r.text)
书面答复如下:

{"asn": 45899, "undetected_urls": [], "undetected_downloaded_samples": [{"date": "2019-07-25 00:55:11", "positives": 0, "total": 70, "sha256": "FDFEFDSFgrgd"}], "country": "VN", "response_code": 1, "as_owner": "VNPT Corp", "verbose_msg": "IP address in dataset", "detected_downloaded_samples": [{"date": "2020-10-13 06:47:34", "positives": 35, "total": 74, "sha256": "dfghdfghdfghdfgh"}], "detected_urls": [{"url": "http://8.8.8.8/", "positives": 3, "total": 79, "scan_date": "2020-10-13 21:27:33"}, {"url": "https://8.8.8.8/", "positives": 3, "total": 79, "scan_date": "2020-10-13 16:10:11"}, {"url": "http://8.8.8.8:49594/Mozi.m", "positives": 2, "total": 79, "scan_date": "2020-10-13 15:47:47"}, {"url": "http://8.8.8.8:49594/Mozi.m/", "positives": 1, "total": 79, "scan_date": "2020-10-13 15:43:04"}], "resolutions": []}
因此,我需要解析的数据以
“detected_url”:
开头,并包含
[{“url”:”http://8.8.8.8/“,“肯定”:3,“总计”:79,“扫描日期”:“2020-10-13 21:27:33”}
{“url”:”https://8.8.8.8/“,“正面”:3,“总体”:79,“扫描日期”:“2020-10-13 16:10:11”}
…等……
它可能是1,2,3-10-20个块,但我需要(注意)只取第一个块,如
[{“url”:”http://8.8.8.8/“,“阳性”:3,“总计”:79,“扫描日期”:“2020-10-13 21:27:33”}
,并解析
“阳性”:3
数据

那么

如果响应中没有
“检测到的URL”:
子句,则必须有
打印“会话已关闭”

如果
“正值”
计数大于
0
,则必须启动以下脚本:

import requests
url = "https://www.example2.com/api/v2/report?apikey=XXXXXXXXXXXXXXX&data=runData"
r = requests.get(url, verify=False)
print(r.status_code)
仅此而已

另外,请使用尽可能少的模块,因为我的系统(使用Phython)可能不支持它们


感谢大家!

就像评论中提到的gold_cy一样,您不需要将数据解析为文本,只需将响应内容转换为JSON即可访问类似于典型词典的信息

因此,您可以这样轻松地完成此任务:

response=requests.get(url,verify=False)
res_json=response.json()
如果len(res_json['detected_url'])==0:
#做事
...
其他:
#从检测到的\u URL获取第一项
first=res_json['detected_url'][0]
#做些事情。。。
r.json()是否返回json对象?看起来文本是有效的json