我開始學習如何使用python請求模塊。對於練我試着管理挑戰/響應的問題:我要訪問的數據上http://lema.rae.es/drae/srv/search?val=hacer使用python請求實現質詢/響應方案
隨着Firefox中的「篡改數據」插件我檢查了必要的HTTP請求:
GET http://lema.rae.es/drae/srv/search?val=hacer
POST http://lema.rae.es/drae/srv/search?val=hacer
我複製在兩個HTTP請求中由Firefox發送精確的標題,並在Python中實現JavaScript「挑戰」功能。然後,我正在執行以下操作:
url = "http://lema.rae.es/drae/srv/search?val=hacer"
headers = { ... }
r1 = requests.get(url=url, headers=headers)
html = r1.content.decode("utf-8")
formdata = challenge(html)
headers = { ... }
r2 = requests.post(url=url, data=formdata, headers=headers)
不幸的是,服務器無法按預期的方式應答。我檢查了所有通過「r.request.headers」發送的頭文件,他們完全同意firefox發送的頭文件(根據Tamper Data)
我在做什麼錯?
你可以在這裏檢查我的全碼:http://pastebin.com/7JAZ9B4s
這是響應頭我應該得到:
Date[Tue, 10 Feb 2015 17:13:53 GMT]
Vary[Accept-Encoding]
Content-Encoding[gzip]
Cache-Control[max-age=0, no-cache]
Keep-Alive[timeout=5, max=100]
Connection[Keep-Alive]
Content-Type[text/html; charset=UTF-8]
Set-Cookie[TS014dfc77=017ccc203c29467c4d9b347fb56ea0e89a7182e52b9d7b4a1174efbf134768569a005c7c85; Path=/]
Transfer-Encoding[chunked]
這是響應頭我真的得到:
Content-Length[5798]
Content-Type[text/html]
Pragma[no-cache]
Cache-Control[no-cache]
通過google找到了。這是行不通的:https://github.com/vibragiel/glotologia/blob/master/enmiendas_drae/enmiendas-drae.py另一方面,以下建議我們需要設置一個cookie:http://stackoverflow.com/questions/26952643/how-to-obtain-a-cookie-from-a-remote-domain-using-greasemonkey但是對於我來說,在瀏覽器中禁用cookie時,頁面也可以工作。 – thomas 2015-02-11 09:06:46
這工作(!):https://github.com/javierhonduco/nebrija我仍然需要找出它是如何工作的... – thomas 2015-02-11 09:46:51
...它總是發送相同的挑戰字符串,它不設置任何cookie還是什麼? – thomas 2015-02-11 09:57:06