简体   繁体   English

为什么请求没有正确获取数据?

[英]Why isn't requests fetching data correctly?

I'm new to the Python requests module and I'm trying to export game data from a user from the Lichess.org API (a chess website). 我是Python请求模块的新手,我正在尝试从Lichess.org API (国际象棋网站)中的用户导出游戏数据。

Here is my code: 这是我的代码:

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"))

The output received below however is missing key components... 但是,下面收到的输出缺少关键组件...

[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

The output above is not providing the information I requested - specifically I set "evals:True" which according to the above documentation means that it ought to have computer analysis values attached to each move eg: 12. Bxf6 { [%eval 0.23] } a3 { [%eval -1.09] } , however that is missing even though I filtered to only games where the analysis is available by setting "analysed":True . 上面的输出未提供我请求的信息-具体来说,我设置了"evals:True" ,根据上述文档,这意味着它应该在每个移动中附加计算机分析值,例如: 12. Bxf6 { [%eval 0.23] } a3 { [%eval -1.09] } ,但是即使我通过设置"analysed":True过滤到仅可进行分析的游戏,也缺少该内容。

Additionally it seems to be ignoring the parameters "clocks":True and "opening":True , in fact the only parameter seemingly being taken into account is "max":2 , why might this be? 另外,它似乎在忽略参数"clocks":True"opening":True ,实际上似乎唯一考虑的参数是"max":2 ,为什么会这样呢?

What am I doing wrong here? 我在这里做错了什么?

The api appears to be case sensitive and is expecting the string "true" in the boolean parameters, all lowercase. 该API似乎区分大小写,并且期望布尔参数中的字符串"true"均为小写。

r = requests.get(url, params={"max": 2, "analysed": "true", "clocks": "true", "evals": "true", "opening": "true"})

Result excerpt: 结果摘录:

(...)

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

When you provide the python boolean value True for these params as in your question, it is ultimately converted to an uppercased string representation, as can be seen in this debug-level log of urllib3 when making the call: 如您在问题中为这些参数提供python布尔值True ,它最终将转换为大写的字符串表示形式,如在进行调用时在urllib3此调试级别日志中可以看到的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM