简体   繁体   English

尝试使用python从URL解析JSON数据

[英]Trying to parse JSON data from a url with python

I am trying to make a IP details grabber via python and a JSON API but I am having trouble parsing the JSON data. 我正在尝试通过python和JSON API制作IP详细信息抓取器,但是在解析JSON数据时遇到了麻烦。

I've tried loading and dumping the data, none of that works so I have 0 idea how I am going to parse this data. 我已经尝试加载和转储数据,但是这些都不起作用,所以我对如何解析此数据有0个想法。

#Importing
import requests
import json
import os

#Variables
cls = os.system('cls')
#Startup

cls #Clearing the console on startup

ipToSearch = input("Please enter the IP you wish to search: ")
saveDetails = input("Would you like to save the IP's deatils to a file? Y/N: ")

ip_JSON = requests.get(url="http://ip-api.com/json/" + ipToSearch).json()
ip_Data = json.loads(ip_JSON)
print(ip_Data)

I am trying to parse the IP's information but the result is this error currently. 我正在尝试解析IP的信息,但结果是当前出现此错误。

Traceback (most recent call last):
  File "main.py", line 16, in <module>
    ip_Data = json.loads(ip_JSON)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\json\__init__.py", line 341, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not dict

The traceback is because it looks like you already converted it to json on the previous line .json() and then you try to do it again. 回溯是因为看起来您已经在前一行.json()上将其转换为json,然后尝试再次进行。

ip_JSON = requests.get(url="http://ip-api.com/json/" + ipToSearch).json()
ip_Data = json.loads(ip_JSON)

Try 尝试

ip_JSON = requests.get(url="http://ip-api.com/json/" + ipToSearch).json()
print(ip_JSON)

try json.dumps, like this 像这样尝试json.dumps

ip_JSON = requests.get(url="http://ip-api.com/json/" + ipToSearch).json()
ip_Data = json.dumps(ip_JSON)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM