如何向OpenSky(飞行跟踪器)发出请求

huangapple go评论75阅读模式
英文:

How do you make a request to OpenSky (flight tracker)

问题

# 导入
import requests
import json

import pandas as pd

# 正方形区域
lon_min = -123.063498
lat_min = -49.082514
lon_max = -123.295758
lat_max = 49.308148

# 请求
user_name = "wvzack"
password = "########"
url_data = "https://" + user_name + ":" + password + "@opensky-network.org/api/states/all?" + 'lamin=' + str(lat_min) + '&lomin=' + str(lon_min) + '&lamax=' + str(lat_max) + '&lomax=' + str(lon_max)
response = requests.get(url_data).json()

# pandas
col_name = ['icao24', 'callsign', 'origin_country', 'time_position', 'last_contact', 'long', 'lat', 'baro_altitude', 'on_ground', 'velocity',
            'true_track', 'vertical_rate', 'sensors', 'geo_altitude', 'squawk', 'spi', 'position_source']

flight_df = pd.DataFrame(response['states'])
flight_df = flight_df.loc[:, 0:16]
flight_df.columns = col_name
flight_df = flight_df.fillna('No Info')
flight_df.head()
print(flight_df)

我已经尝试更改登录代码,但没有什么变化。我得到了这个错误:

ValueError: 长度不匹配:期望的轴有0个元素,新值有17个元素 或这个错误:

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='wvzack', port=443): Max retries exceeded with url: / (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x000002079A603450>: Failed to resolve 'wvzack' ([Errno 11001] getaddrinfo failed)")(有时候我会只得到其中一个,有时候会两个)。抱歉如果这个帖子没有正确发布,这是我第一次使用stackoverflow。


<details>
<summary>英文:</summary>

I am making a flight tracker that will track planes within certain areas around airports. 
This is my code so far (a modified tutorial) 


#import
import requests
import json

import pandas as pd

#square shape
lon_min=-123.063498
lat_min=-49.082514
lon_max=-123.295758
lat_max=49.308148

#requests
user_name="wvzack"
password="########"
url_data="https://" + user_name + ":" + password + "@opensky-network.org/api/states/all?"+'lamin=' + str(lat_min) +'&lomin=' + str(lon_min) + '&lamax=' + str(lat_max) + '&lomax=' + str(lon_max)
response=requests.get(url_data).json()

#pandas
col_name=['icao24','callsign','origin_country','time_position','last_contact','long','lat','baro_altitude','on_ground','velocity',
'true_track','vertical_rate','sensors','geo_altitude','squawk','spi','position_source']

flight_df=pd.DataFrame(response['states'])
flight_df=flight_df.loc[:,0:16]
flight_df.columns = col_name
flight_df=flight_df.fillna('No Info')
flight_df.head()
print(flight_df)



I have tried changing the login code but nothing really happened. I am getting this error:

`ValueError: Length mismatch: Expected axis has 0 elements, new values have 17 elements` 
or this error:

 `requests.exceptions.ConnectionError: HTTPSConnectionPool(host=&#39;wvzack&#39;, port=443): Max retries exceeded with url: / (Caused by NameResolutionError(&quot;&lt;urllib3.connection.HTTPSConnection object at 0x000002079A603450&gt;: Failed to resolve &#39;wvzack&#39; ([Errno 11001] getaddrinfo failed)&quot;))` 
(I just get one of these sometimes both). 
Sorry if this post is not made properly this is my first time using stackoverflow.

</details>


# 答案1
**得分**: 1

第二个错误是因为你的URL格式不正确。主机名不应该是你的用户名。
不要在URL中传递用户名和密码,而是使用HTTPBasicAuth方法传递认证信息,像这样:

```python
import requests
from requests.auth import HTTPBasicAuth

# requests
user_name = "wvzack"
password = "########"
basic = HTTPBasicAuth('user', 'pass')

url_data = "https://opensky-network.org/api/states/all?" + 'lamin=' + str(lat_min) + '&lomin=' + str(lon_min) + '&lamax=' + str(lat_max) + '&lomax=' + str(lon_max)
response = requests.get(url_data, auth=basic).json()

获取数据并检查你得到的内容。关于17个特征的错误是因为你有0列,试图将它们重命名为一组17个列,这意味着数据框是空的。

当你正确获取数据时,这个问题应该会解决。

英文:

The second error is because you url is malformed. the host should not be your username.
Instead of passing you user and password in the url, use the HTTPBasicAuth method to pass the auth info, like this:

import requests
from requests.auth import HTTPBasicAuth

#requests
user_name=&quot;wvzack&quot;
password=&quot;########&quot;
basic = HTTPBasicAuth(&#39;user&#39;, &#39;pass&#39;)

url_data=&quot;https://opensky-network.org/api/states/all?&quot;+&#39;lamin=&#39; + str(lat_min) +&#39;&amp;lomin=&#39; + str(lon_min) + &#39;&amp;lamax=&#39; + str(lat_max) + &#39;&amp;lomax=&#39; + str(lon_max)
response=requests.get(url_data, auth=basic).json()

to get the data, and inspect what you get. The error you get about the 17 features is because you have 0 columns and trying to rename them to a set of 17, which means the dataframe is empty.

This should be resolved when you actually get the data properly.

huangapple
  • 本文由 发表于 2023年6月22日 00:05:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定