簡體   English   中英

嘗試使用python從URL解析JSON數據

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

我正在嘗試通過python和JSON API制作IP詳細信息抓取器,但是在解析JSON數據時遇到了麻煩。

我已經嘗試加載和轉儲數據,但是這些都不起作用,所以我對如何解析此數據有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)

我正在嘗試解析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

回溯是因為看起來您已經在前一行.json()上將其轉換為json,然后嘗試再次進行。

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

嘗試

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

像這樣嘗試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