Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

discord - how to get a apis output in python without json

here is api i am trying https://api.apithis.net/host2ip.php?hostname=google.com as you can see unlike json apis it dose not have "" that you can copy as response.json

@client.command()
async def host(ctx, host):
    url = ('https://api.apithis.net/host2ip.php?hostname=' + host)

    response = requests.get(url)
    ipaddress = response.json()

    embed = discord.Embed(title="IP for website" + host, color=0x00ffff)
    embed.add_field(name="IP:", value=f'{ipaddress}', inline=True)                                                            
    
    await ctx.send(embed=embed)

here is my current code if anyone can fix would help alot as im not sure how to use apis without json output thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Firstly the response you are getting is not JSON. If you see the response header then you can see it is content-type: text/html; charset=UTF-8.

Now that it is not JSON you have to treat it as text.

r = requests.get(url)
ipaddress = r.text if r.status_code == 200 else ''

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...