为什么请求无法正确获取数据?

时间:2019-01-14 23:32:09

标签: python python-requests

我是Python请求模块的新手,我正尝试从Lichess.org API(国际象棋网站)中导出用户的游戏数据。

这是我的代码:

import requests

url = "https://www.lichess.org/api/games/user/mbellm"

r = requests.get(url, params={"max":2, "analysed":True, "clocks":True, "evals":True, "opening":True})
print(r.content.decode("utf-8"))

但是,下面收到的输出缺少关键组件...

[Event "Rated Bullet game"]
[Site "https://lichess.org/IqlbjkHX"]
[Date "2019.01.13"]
[Round "-"]
[White "mbellm"]
[Black "Ruediruempel"]
[Result "0-1"]
[UTCDate "2019.01.13"]
[UTCTime "22:45:48"]
[WhiteElo "1097"]
[BlackElo "1202"]
[WhiteRatingDiff "-8"]
[BlackRatingDiff "+7"]
[Variant "Standard"]
[TimeControl "60+0"]
[ECO "C50"]
[Termination "Normal"]

1. e4 e5 2. Nf3 Nf6 3. Bc4 Nc6 4. d3 Bc5 5. Nc3 Nd4 6. O-O O-O 7. Nxd4 Bxd4 8. Nd5 Nxd5 9. Bxd5 c6 10. c3 cxd5 11. cxd4 dxe4 12. Qg4 exd3 13. dxe5 d6 14. Bd2 Bxg4 15. exd6 Qxd6 16. f3 Bf5 17. Rad1 h6 18. a3 Bh7 19. Bb4 Qd4+ 20. Kh1 Qxb2 21. Rxd3 Bxd3 22. Rc1 Qxc1+ 23. Be1 Qxe1# 0-1


[Event "Rated Classical game"]
[Site "https://lichess.org/sgNWdvkn"]
[Date "2019.01.13"]
[Round "-"]
[White "Stalingrad_1"]
[Black "mbellm"]
[Result "1-0"]
[UTCDate "2019.01.13"]
[UTCTime "04:15:39"]
[WhiteElo "1656"]
[BlackElo "1732"]
[WhiteRatingDiff "+13"]
[BlackRatingDiff "-13"]
[Variant "Standard"]
[TimeControl "900+15"]
[ECO "D00"]
[Termination "Normal"]

1. d4 d5 2. e3 Nf6 3. c4 c6 4. Qb3 Nbd7 5. Nc3 Nb6 6. cxd5 Nfxd5 7. Nxd5 cxd5 8. Bb5+ Bd7 9. Nf3 e6 10. Bxd7+ Qxd7 11. Ne5 Qc7 12. Bd2 Bd6 13. Rc1 Qe7 14. Qb5+ Kf8 15. f4 Bxe5 16. dxe5 Nc4 17. Bb4 1-0

上面的输出未提供我要求的信息-具体来说,我设置了"evals:True",根据上述文档,这意味着它应该在每个移动中都附加计算机分析值,例如:12. Bxf6 { [%eval 0.23] } a3 { [%eval -1.09] }即使我通过设置"analysed":True仅过滤到可以进行分析的游戏,也缺少该信息。

另外,它似乎忽略了参数"clocks":True"opening":True,实际上似乎唯一考虑的参数是"max":2,为什么会这样呢?

我在这里做什么错了?

1 个答案:

答案 0 :(得分:4)

api似乎区分大小写,并且期望布尔参数中的字符串r = requests.get(url, params={"max": 2, "analysed": "true", "clocks": "true", "evals": "true", "opening": "true"}) 均为小写。

True

结果摘录:

(...)

1. e4 { [%eval 0.12] [%clk 0:15:00] } 1... c5 { [%eval 0.18] [%clk 0:15:00] } 2. Nf3 { [%eval 0.19] [%clk 0:15:13] } 2... Nc6 { [%eval 0.08] [%clk 0:15:13] } 3. Bc4 { [%eval -0.2] [%clk 0:15:26] } 3... e6 { [%e
val 0.0] [%clk 0:15:06] } 4. Nc3 { [%eval -0.21] [%clk 0:15:37] } 4... Nf6 { [%eval -0.23] [%clk 0:15:12] } 5. d3 { [%eval -0.2] [%clk 0:14:53] } 5... d5 { [%eval -0.09] [%clk 0:15:13] } 6. exd5 { [%eval -0.53]
 [%clk 0:14:25] } 6... exd5 { [%eval -0.43] [%clk 0:15:21] } 7. Bb3 { [%eval -0.54] [%clk 0:14:27] } 7... Be7 { [%eval -0.31] [%clk 0:15:06] } 8. O-O { [%eval -0.37] [%clk 0:14:27] } 8... O-O { [%eval -0.41] [%
clk 0:15:19] } 9. a3 { [%eval -0.57] [%clk 0:14:02] } 9... Be6 { [%eval -0.52] [%clk 0:15:18] } 10. Ng5 { [%eval -1.72] [%clk 0:13:51] } 10... Nd4 { [%eval -0.68] [%clk 0:14:51] } 11. Nxe6 { [%eval -0.71] [%clk
 0:13:42] } 11... Nxe6 { [%eval -0.28] [%clk 0:15:01] } 12. Qf3 { [%eval -0.99] [%clk 0:13:27] } 12... Nd4 { [%eval -0.82] [%clk 0:14:53] } 13. Qd1 { [%eval -0.91] [%clk 0:11:51] } 13... Nxb3 { [%eval -0.79] [%
clk 0:15:04] } 14. cxb3 { [%eval -1.09] [%clk 0:12:00] } 14... a6 { [%eval -0.1] [%clk 0:14:08] } 15. d4 { [%eval -0.12] [%clk 0:11:15] } 15... h6 { [%eval 0.0] [%clk 0:13:24] } 16. dxc5 { [%eval -0.43] [%clk 0
:11:03] } 16... Bxc5 { [%eval -0.36] [%clk 0:13:37] } 17. Na4 { [%eval -1.18] [%clk 0:09:25] } 17... Rc8 { [%eval 0.09] [%clk 0:13:20] } 18. Nxc5 { [%eval 0.0] [%clk 0:09:20] } 18... Rxc5 { [%eval 0.06] [%clk 0
:13:29] } 19. Be3 { [%eval 0.0] [%clk 0:09:29] } 19... Rc8 { [%eval 0.16] [%clk 0:12:49] } 20. Qd4 { [%eval 0.14] [%clk 0:09:17] } 20... b5 { [%eval 0.31] [%clk 0:12:01] } 21. Rac1 { [%eval 0.23] [%clk 0:08:36]
 } 21... Qd6 { [%eval 0.36] [%clk 0:11:33] } 22. Bf4 { [%eval 0.2] [%clk 0:08:09] } 22... Qe6 { [%eval 0.13] [%clk 0:10:58] } 23. Rfe1 { [%eval 0.0] [%clk 0:07:37] } 23... Rxc1 { [%eval 0.03] [%clk 0:11:06] } 2
4. Rxc1 { [%eval 0.09] [%clk 0:07:45] } 24... Rc8 { [%eval 0.08] [%clk 0:10:36] } 25. h3 { [%eval -6.7] [%clk 0:06:39] } 25... Rxc1+ { [%eval -6.58] [%clk 0:10:46] } 26. Bxc1 { [%eval -6.37] [%clk 0:06:52] } 26
... Qe1+ { [%eval -6.21] [%clk 0:10:57] } 0-1

当按照您的问题为这些参数提供python布尔值urllib3时,它最终将转换为大写的字符串表示形式,如在{{1}}的调试级别日志中所见,拨打电话:

DEBUG:urllib3.connectionpool:https://lichess.org:443 "GET /api/games/user/mbellm?max=2&analysed=True&clocks=True&evals=True&opening=True HTTP/1.1" 200 None