有人可以解释这个 "request" 代码是如何工作的吗?
Can someone explain how this "request" code works?
我正在使用以下代码从 coinbase 获取 BTC 数据。我只是从教程中复制了这个,但我想知道它是如何工作的。有人可以一步一步解释吗?谢谢
import requests # imports the request library
url = "https://api.exchange.coinbase.com/products/BTC-USD/book?level=1" # provides the url from which data will be pulled
headers = {"Accept": "application/json"} # I don't understand the point of this line
response = requests.request("GET", url, headers=headers) # I just don't understand the point of the 'header' part
print(response.text) # This just prints the result
这是上面代码的结果:
{"bids":[["45890.49","0.02316146",1]],
"asks":[["45896.07","0.03631374",1]],
"sequence":35929645093,
"auction_mode":false,
"auction":null}
我了解所有这些数据,只是不知道代码如何获取这些数据
您的代码通过附加 HTTP Headers:
向 https://api.exchange.coinbase.com/products/BTC-USD/book?level=1
发送 GET
请求
Accept: application/json
这会导致网络服务器在 JSON-Format 中发送您的查询结果,您可以使用 response.text
访问它
我正在使用以下代码从 coinbase 获取 BTC 数据。我只是从教程中复制了这个,但我想知道它是如何工作的。有人可以一步一步解释吗?谢谢
import requests # imports the request library
url = "https://api.exchange.coinbase.com/products/BTC-USD/book?level=1" # provides the url from which data will be pulled
headers = {"Accept": "application/json"} # I don't understand the point of this line
response = requests.request("GET", url, headers=headers) # I just don't understand the point of the 'header' part
print(response.text) # This just prints the result
这是上面代码的结果:
{"bids":[["45890.49","0.02316146",1]],
"asks":[["45896.07","0.03631374",1]],
"sequence":35929645093,
"auction_mode":false,
"auction":null}
我了解所有这些数据,只是不知道代码如何获取这些数据
您的代码通过附加 HTTP Headers:
向https://api.exchange.coinbase.com/products/BTC-USD/book?level=1
发送 GET
请求
Accept: application/json
这会导致网络服务器在 JSON-Format 中发送您的查询结果,您可以使用 response.text