


Why can't my code get the data returned by the API? How to solve this problem?
Troubleshooting and solving the problem that Python code cannot obtain the data returned by API
This article analyzes a Python code case that returns empty values when calling ip-api.com
API using the requests
library and provides a solution.
Problem description
The following code tries to get the batch IP information of ip-api.com
using a POST request, but always returns a null value:
import random, requests, json ip = [ "49.104.25.257", "39.115.131.116" ] api = "http://ip-api.com/batch?fields=58898?lang=zh_cn" headers = { "user-agent": "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/119.0.0.0 safari/537.36 edg/119.0.0.0", "content-type":"application/json" } ip = json.dumps(ip, ensure_ascii=False, indent=2) print(ip) resp = requests.post(url=api,data=ip,headers=headers) print(resp.status_code) print(resp.text) resp.close()
This API is a free version with a limit of 45 requests per minute, using POST requests, fields=58898
is used to specify the return field. Although there is no problem in querying the IP directly on the API website, the code always returns a null value and requests
library does not report an error.
Problem analysis and solutions
After carefully checking the code and API documentation, I found that the problem is the query parameter settings of the URL. In the original code, the URL is:
api = "http://ip-api.com/batch?fields=58898?lang=zh_cn"
&
connection should be used between multiple query parameters, rather than continuous use ?
The correct URL should be:
api = "http://ip-api.com/batch?fields=58898&lang=zh_CN"
In addition, it is recommended to set the lang
parameter to zh_CN
to ensure that Chinese data is returned. At the same time, User-Agent
and content-type
in HTTP Header should use the standard naming method, with the initials capitalized.
Here is the modified complete code:
import requests import json ip = [ "49.104.25.257", "39.115.131.116" ] api = "http://ip-api.com/batch?fields=58898&lang=zh_CN" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0", "Content-Type": "application/json" } ip = json.dumps(ip, ensure_ascii=False, indent=2) print(ip) resp = requests.post(url=api, data=ip, headers=headers) print(resp.status_code) print(resp.text) resp.close()
By modifying the URL and HTTP Header, the API returns data can be successfully obtained. This example illustrates the potential failure of a subtle syntax error in API calls, emphasizing the importance of double-checking URLs and parameters.
The above is the detailed content of Why can't my code get the data returned by the API? How to solve this problem?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Do you want to know how to display child categories on the parent category archive page? When you customize a classification archive page, you may need to do this to make it more useful to your visitors. In this article, we will show you how to easily display child categories on the parent category archive page. Why do subcategories appear on parent category archive page? By displaying all child categories on the parent category archive page, you can make them less generic and more useful to visitors. For example, if you run a WordPress blog about books and have a taxonomy called "Theme", you can add sub-taxonomy such as "novel", "non-fiction" so that your readers can

Factors of rising virtual currency prices include: 1. Increased market demand, 2. Decreased supply, 3. Stimulated positive news, 4. Optimistic market sentiment, 5. Macroeconomic environment; Decline factors include: 1. Decreased market demand, 2. Increased supply, 3. Strike of negative news, 4. Pessimistic market sentiment, 5. Macroeconomic environment.

Understand the randomness of circular dependencies in Spring project startup. When developing Spring project, you may encounter randomness caused by circular dependencies at project startup...

Discussion on Hierarchical Structure in Python Projects In the process of learning Python, many beginners will come into contact with some open source projects, especially projects using the Django framework...

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Why is the return value empty when using RedisTemplate for batch query? When using RedisTemplate for batch query operations, you may encounter the returned results...

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.
