Python post请求失败3

时间:2016-10-30 08:54:03

标签: python-requests

我正在尝试使用python requests库发布帖子请求。我试图复制的帖子在chrome开发人员工具中看起来像这样:

:path:/leagueOverview/football/england/premier-league?requestId=5&wsTrack=441d62c066a64465a2f7b03717d2d694&method=post
:scheme:https
accept:application/json
accept-encoding:gzip, deflate, br
accept-language:da-DK,da;q=0.8,en-US;q=0.6,en;q=0.4
content-length:6019
content-type:application/x-www-form-urlencoded

user-agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36
Query String Parameters
view source
view URL encoded
requestId:5
wsTrack:441d62c066a64465a2f7b03717d2d694
method:post
Form Data
view source
view URL encoded
bettingTypeIds:7
bettingTypeIds:8
bettingTypeIds:9

我的代码有错误就像这样

headerbb = {'accept':'application/json' ,\
'accept-encoding':'gzip, deflate, br',\
'accept-language':'da-DK,da;q=0.8,en-US;q=0.6,en;q=0.4',\

'content-length':'6019',\
'content-type':'application/x-www-form-urlencoded',\
'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) \
          Chrome/54.0.2840.71 Safari/537.36'}

session = requests.Session()  
url1 = 'https://bbfeapi.betbrain.com/leagueOverview/football/england    /premier-league?requestId=5'
url2 = url1+'&wsTrack=' + wsTrack+'&method=post'
formdata = {'bettingTypeIds': ['7','8','9'] }

session.post(url2, headers=headerbb,data=formdata )

这给了我一个错误代码500。我的猜测是我在formdata做错了什么,但我怎么以正确的方式写这个formdada?我可能也在标题

中做错了

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我不确定您到底想要做什么,但这里有一个简单的方法来复制以chrome记录的HTTP请求:

在Chrome开发者工具中,右键点击要复制的请求,然后点击" 复制为cURL ",然后转到here,选择"蟒"作为curl-command中的语言和粘贴。 这将输出应该准备好的python(-requests)代码。如果需要session-cookies,您可能需要更改代码以使用请求会话(如您在示例中所做的那样)并在执行POST之前请求一些页面。

如果这不能帮助您找到解决方案,请修改您的问题以包含您从Chrome中复制的cURL。

修改

请尝试使用此代码,看看它是否适合您。

import requests, json
from pprint import pprint

headers = {
    'pragma': 'no-cache',
    'origin': 'https://www.betbrain.com',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-GB,en;q=0.8,en-US;q=0.6,de;q=0.4',
    'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
    'content-type': 'application/x-www-form-urlencoded',
    'accept': 'application/json',
    'cache-control': 'no-cache',
    'authority': 'bbfeapi.betbrain.com',
    'cookie': 'visid_incap_878444=0IWvPeyoSiOyl15h8NeUkmcBFlgAAAAAQUIPAAAAAACClzFL2y3zI3kGh0UF9iBb; incap_ses_289_878444=CLx3JSblzwMcgYYoVbwCBGcBFlgAAAAA9gzkJrI33q8daHr8iP7XrQ==; JSESSIONID=AF30CFE0653AECB1A20CFACD686E8A4F; incap_ses_184_878444=FRwnZDjTa0wAxRtNVOWNAhQYGVgAAAAAOjOVH1A2ipnR7gEwfzvGsg==; oddsFormat=EU; bookieNameAndLogo=NAME_LOGO; showOddsWithCommission=false; gmtTimezoneOffset=0',
    'referer': 'https://www.betbrain.com/football/england/premier-league/',
}

data = {
  'bettingTypeIds': '7,8,9,11,12,13,24,25,26,27,28,29,30,32,33,34,35,37,39,45,47,48,49,50,51,52,53,57,60,62,63,64,65,66,67,68,69,70,71,75,76,77,78,79,80,81,82,83,84,85,87,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304,305,306,307,308,309,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358',
  'domain': 'www.betbrain.com'
}
parameters = {
    "requestId": 1,
    "wsTrack": "eabad591a6c44ecf94a81287ba246920",
    "method": "post"
}
response = requests.post('https://bbfeapi.betbrain.com/leagueOverview/football/england/premier-league?', params=parameters, headers=headers, data=data)
print response.status_code
pprint(json.loads(response.text))