有人可以解释一下这个"request"代码是如何工作的吗?



我使用以下代码从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}

我了解所有这些数据,只是不了解代码是如何获得的

您的代码向https://api.exchange.coinbase.com/products/BTC-USD/book?level=1发送一个带有附加HTTP头的GET请求:

Accept: application/json

这导致Web服务器以JSON格式发送查询结果,您可以使用response.text访问该格式

最新更新