我想用IP解析跟随JSON的问题,并将其计数到一个只包含使用Python的IP的列表中。
&安培;代码:`
response_ips= requests.get(url=link_getResults,headers=payload)
response_ips.raise_for_status()
data_ips= json.loads(response_ips.text)
results=data_ips['results']
print results
答案 0 :(得分:1)
将results
拆分为\r\n
以获取每一行,然后将\t
上的每一行拆分,以将IP与计数分开。
ips = [line.split("\t")[0] for line in results.split("\r\n")]
print(ips)
答案 1 :(得分:0)
由于您拥有JSON字符串,因此首先需要将其转换为dict
。转换后,您需要转义\r\n
否则JSON解码将引发错误(您的问题中的代码将抛出错误)。然后,您需要在results
上分割\r\n
,然后在\t
分割以获取IP。
请注意,在您的问题中,输入未包含在引号中。它应该是JSON。
import json
from functools import partial
from operator import is_not
j = '{"inline":true,"results":"168.9.64.246\t65098\r\n188.68.184.188, 10.178.156.22\t61168\r\n188.68.184.188, 10.178.156.58\t61050\r\n188.68.184.188, 10.178.156.24\t61039\r\n188.68.184.188, 10.178.156.31\t61038\r\n188.68.184.188, 10.178.156.35\t61036\r\n188.68.184.188, 10.178.156.44\t61023\r\n188.68.184.188, 10.178.156.34\t61013\r\n188.68.184.188, 10.178.156.71\t61012\r\n188.68.184.188, 10.178.156.33\t61008\r\n188.68.184.188, 10.178.156.40\t60985\r\n188.68.184.188, 10.178.156.36\t60976\r\n188.68.184.188, 10.178.156.43\t60974\r\n188.68.184.188, 10.178.156.26\t60962\r\n188.68.184.188, 10.178.156.61\t60960\r\n188.68.184.188, 10.178.156.66\t60958\r\n188.68.184.188, 10.178.156.68\t60956\r\n188.68.184.188, 10.178.156.52\t60942\r\n188.68.184.188, 10.178.156.49\t60940\r\n188.68.184.188, 10.178.156.53\t60921\r\n188.68.184.188, 10.178.156.48\t60898\r\n188.68.184.188, 10.178.156.45\t60857\r\n188.68.184.188, 10.178.156.29\t60851\r\n188.68.184.188, 10.178.156.69\t60844\r\n188.68.184.188, 10.178.156.74\t60840\r\n188.68.184.188, 10.178.156.60\t60838\r\n188.68.184.188, 10.178.156.55\t60834\r\n188.68.184.188, 10.178.156.63\t60829\r\n188.68.184.188, 10.178.156.30\t60827\r\n188.68.184.188, 10.178.156.67\t60825\r\n188.68.184.188, 10.178.156.62\t60814\r\n188.68.184.188, 10.178.156.64\t60793\r\n188.68.184.188, 10.178.156.50\t60784\r\n188.68.184.188, 10.178.156.54\t60781\r\n188.68.184.188, 10.178.156.25\t60777\r\n188.68.184.188, 10.178.156.39\t60759\r\n188.68.184.188, 10.178.156.65\t60758\r\n188.68.184.188, 10.178.156.42\t60750\r\n188.68.184.188, 10.178.156.73\t60744\r\n188.68.184.188, 10.178.156.51\t60731\r\n188.68.184.188, 10.178.156.27\t60725\r\n188.68.184.188, 10.178.156.23\t60718\r\n188.68.184.188, 10.178.156.28\t60715\r\n188.68.184.188, 10.178.156.37\t60703\r\n188.68.184.188, 10.178.156.72\t60690\r\n"}'
d = json.loads(j)
ips = list(filter(partial(is_not, ''),[ip.split('\t')[0] for ip in d['results'].split('\r\n')]))
以上代码输出:
['168.9.64.246', '188.68.184.188, 10.178.156.22', '188.68.184.188, 10.178.156.58', '188.68.184.188, 10.178.156.24', '188.68.184.188, 10.178.156.31', '188.68.184.188, 10.178.156.35', '188.68.184.188, 10.178.156.44', '188.68.184.188, 10.178.156.34', '188.68.184.188, 10.178.156.71', '188.68.184.188, 10.178.156.33', '188.68.184.188, 10.178.156.40', '188.68.184.188, 10.178.156.36', '188.68.184.188, 10.178.156.43', '188.68.184.188, 10.178.156.26', '188.68.184.188, 10.178.156.61', '188.68.184.188, 10.178.156.66', '188.68.184.188, 10.178.156.68', '188.68.184.188, 10.178.156.52', '188.68.184.188, 10.178.156.49', '188.68.184.188, 10.178.156.53', '188.68.184.188, 10.178.156.48', '188.68.184.188, 10.178.156.45', '188.68.184.188, 10.178.156.29', '188.68.184.188, 10.178.156.69', '188.68.184.188, 10.178.156.74', '188.68.184.188, 10.178.156.60', '188.68.184.188, 10.178.156.55', '188.68.184.188, 10.178.156.63', '188.68.184.188, 10.178.156.30', '188.68.184.188, 10.178.156.67', '188.68.184.188, 10.178.156.62', '188.68.184.188, 10.178.156.64', '188.68.184.188, 10.178.156.50', '188.68.184.188, 10.178.156.54', '188.68.184.188, 10.178.156.25', '188.68.184.188, 10.178.156.39', '188.68.184.188, 10.178.156.65', '188.68.184.188, 10.178.156.42', '188.68.184.188, 10.178.156.73', '188.68.184.188, 10.178.156.51', '188.68.184.188, 10.178.156.27', '188.68.184.188, 10.178.156.23', '188.68.184.188, 10.178.156.28', '188.68.184.188, 10.178.156.37', '188.68.184.188, 10.178.156.72']
注意:您还需要从列表中过滤''
,因为\r\n
上的拆分会产生''
作为最后一个元素。