2017-01-07 94 views
0

我有一個應用程序,我在我的手機上發送帖子請求並獲取一些信息。我試圖模仿來自Python腳本的發佈請求,並獲取相同的響應。在Python中模仿發佈請求

我用了一個數據包嗅探器,看看該請求是什麼:

POST /b/ss/wdgwespmaflm/0/JAVA-4.10.0-AN/s23376784 HTTP/1.1 
connection: close 
Content-Type: application/x-www-form-urlencoded 
User-Agent: Mozilla/5.0 (Linux; U; Android 5.1; en-US; HTC One_M8 Build/LMY47O.H4) 
Accept-Language: en-US 
Content-Length: 530 
Host: w88.espn.com 
Accept-Encoding: gzip 

ndh=1&ce=UTF-8&vid=5f2c4950ca6e9359&pev2=ADBINTERNAL%3ALifecycle&c.&a.&DaysSinceFirstUse=52&RunMode=Application&HourOfDay=12&Resolution=1080x1776&DaysSinceLastUse=0&CarrierName=Partner&AppID=ESPN%20Fantasy%205.1.1%20%28155%29&TimeSinceLaunch=3&OSVersion=Android%205.1&PrevSessionLength=17&Launches=73&DeviceName=HTC%20One_M8&DayOfWeek=7&LaunchEvent=LaunchEvent&internalaction=Lifecycle&.a&.c&t=00%2F00%2F0000%2000%3A00%3A00%200%20-120&mid=61999711066602377445137974387884905365&pe=lnk_o&pageName=ESPN%20Fantasy%205.1.1%20%28155%29 

我使用的模塊requests,我想這

r = requests.post("http://w88.espn.com/b/ss/wdgwespmaflm/0/JAVA-4.10.0-AN/s23376784", headers={ 
    'connection': 'close', 
    'Content-Type': 'application/x-www-form-urlencoded', 
    'User-Agent': 'Mozilla/5.0 (Linux; U; Android 5.1; en-US; HTC One_M8 Build/LMY47O.H4)', 
    'Accept-Language': 'en-US', 
    'Content-Length': '530', 
    'Host': 'w88.espn.com', 
    'Accept-Encoding': 'gzip'}) 

但腳本只是被卡住的要求。我在哪裏發送內容? 我是不是應該把它作爲headers發送?我試過data,它也沒有工作。

有什麼想法?

+0

首先,您的網址是不是完成「/b/ss/wdgwespmaflm/0/JAVA-4.10.0-AN/s23376784」,缺少協議和主機。 –

+0

使用'http:// w88.espn.com/b/ss/wdgwespmaflm/0/JAVA-4.10.0-AN/s23376784'作爲'URL' – Andersson

+0

嗯好吧,我會試試 – Mumfordwiz

回答

0

據我所知,你的數據包嗅探器的最後一行是你應該傳遞給服務器的參數。儘量串聯schema + host + resource locator + parameters

url = http://www.w88.espn.com/b/ss/wdgwespmaflm/0/JAVA-4.10.0-AN/s23376784?ndh=1&ce=UTF-8&vid=5f2c4950ca6e9359&pev2=ADBINTERNAL%3ALifecycle&c.&a.&DaysSinceFirstUse=52&RunMode=Application&HourOfDay=12&Resolution=1080x1776&DaysSinceLastUse=0&CarrierName=Partner&AppID=ESPN%20Fantasy%205.1.1%20%28155%29&TimeSinceLaunch=3&OSVersion=Android%205.1&PrevSessionLength=17&Launches=73&DeviceName=HTC%20One_M8&DayOfWeek=7&LaunchEvent=LaunchEvent&internalaction=Lifecycle&.a&.c&t=00%2F00%2F0000%2000%3A00%3A00%200%20-120&mid=61999711066602377445137974387884905365&pe=lnk_o&pageName=ESPN%20Fantasy%205.1.1%20%28155%29 

或添加這些參數作爲params參數:

params = {'ndh': '1', 
      'ce': 'UTF-8', 
      'vid': '5f2c4950ca6e9359', ...} 

,並把您的要求爲

r = requests.post(url, params=params, headers=headers) 
+0

嘗試在所有方面..將無法正常工作..感謝您的幫助! – Mumfordwiz

+0

本網站是否需要授權才能執行您的請求? – Andersson

+0

我不這麼認爲,這是嗅探器從這個應用程序記錄的第一個數據包。 – Mumfordwiz