您的代码运行平稳,在我的电脑:
>>> requests.post('http://results.nyrr.org/api/runners/resultDetails', {'runnerId': 11585036})
<Response [200]>
所以我只能猜测,为什么你的代码不能正常工作。
下面是浏览器中的正常请求:
POST http://results.nyrr.org/api/runners/resultDetails HTTP/1.1
Host: results.nyrr.org
Connection: keep-alive
Content-Length: 21
Accept: application/json, text/plain, */*
Username: subscriber
Origin: http://results.nyrr.org
Password: umPrcNcZKuJ9TQ2
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://results.nyrr.org/event/M2016/result/2
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8,en;q=0.6
Cookie: __cfduid=d518b5aad8e36a9f400b27bbf4355f42e1503820652; ARRAffinity=d217e26f983a10c0f58203f7743dc44212160b2fbe97e9354b4a7b599e51a221; __atuvc=2%7C35; __atuvs=59a27b785f22cb33001
{"runnerId":11585036}
与Python代码发送的请求相比,我们可以看到以下区别:
- 的正常请求发送使用JSON参数,而你的代码以表格格式发送参数。
- 正常的请求包括一些标头,如
User-Agent
和Referer
,这些标头通常用于执行反铲。
- 正常的请求有一个由JS代码生成的
Username
和Password
头。绝对是用于反刮,但我不知道为什么这不起作用。
然后这里是建议我可以给你:
- 发送参数以JSON格式。像:
requests.post('http://...', json={'runnerId': 11585036})
- 添加更多标头。如:
requests.post('http://...', json=..., headers={'User-Agent':'...', ...})
- 使用
selenium
如果您仍然被反刮技术禁用。
你的建议工作:) – Belisario