在Python中模仿发布请求

时间:2017-01-07 10:49:05

标签: python python-2.7 http post python-requests

我有一个我在手机上使用的应用程序,它会发送帖子请求并获取一些信息。我试图模仿来自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'})

但脚本只是卡在请求上。我在哪里发送内容? 我不应该以{{1​​}}发送所有内容吗?我尝试headers,它也没有工作。

有什么想法吗?

1 个答案:

答案 0 :(得分: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)