使用python中的请求发出发布请求

时间:2018-12-30 09:59:52

标签: python parameters request-headers

我正在尝试向URL-https://www.shoppersstop.com/cart/add发出发布请求。这是将商品添加到购物车。 标题如下。

{'Host': 'www.shoppersstop.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': 'https://www.shoppersstop.com/haute-curry-womens-tie-up-neck-printed-churidar-suit/p-203566837',
'X-NewRelic-ID': 'VQEOVVBVDhAHUVVUBgcEVA==',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest',
'Content-Length': '104',
'Connection': 'keep-alive',
'Cookie': 'JSESSIONID=4170ADCF31D6DCC1BDCB844BCFF47B1B; ROUTEID=.node5; _dy_csc_ses=t; _dy_ses_load_seq=24547%3A1546102645178; _dy_c_exps=; _dy_soct=360836.597881.1546100027*338160.547739.1546102645*360599.597438.1546102646; AMCV_E71BF792598CD6610A495DB7%40AdobeOrg=1406116232%7CMCIDTS%7C17895%7CMCMID%7C82500003709226011980164123367731652803%7CMCAAMLH-1546706214%7C3%7CMCAAMB-1546706214%7CRKhpRz8krg2tLO6pguXWp5olkAcUniQYPHaMWWgdJ3xzPWQmdj0y%7CMCOPTOUT-1546108614s%7CNONE%7CMCAID%7C2E0BC192052A2F40-60000107E00320EA%7CvVersion%7C2.5.0; G_ENABLED_IDPS=google; AMCVS_E71BF792598CD6610A495DB7%40AdobeOrg=1; _dycnst=dg; s_ppvl=home%257Ccheckout%257Ccart%2520contents%2C39%2C39%2C585%2C1280%2C585%2C1280%2C720%2C1.5%2CP; s_ppv=home%257Cwomen%257Cindianwear%257Csalwar%2520%2526%2520churidar%2520suits%257Chaute%2520curry%2520womens%2520tie%2520up%2520neck%2520printed%2520churidar%2520suit%2C27%2C27%2C1224%2C780%2C585%2C1280%2C720%2C1.5%2CP; s_cc=true; _dyid=4391010063425549884; _dyjsession=6d4332784aa30af25e9ac885d7a9c8da; _dycst=dk.w.f.ms.frv5.ltos.; _dyus_8770829=701%7C4%7C0%7C0%7C0%7C0.0.1545908198200.1546102647338.194449.0%7C362%7C52%7C11%7C118%7C26%7C13%7C7%7C0%7C0%7C0%7C0%7C46%7C0%7C0%7C0%7C0%7C0%7C46%7C0%7C0%7C0%7C0%7C0; _dy_geo=IN.AS.IN_19.IN_19_Bangalore; _dy_df_geo=India..Bangalore; _dy_toffset=-4; fmf.userData=%7B%22userName%22%3A%22snfuser1545908198616%40device.com%22%7D; AMCV_E71BF792598CD6610A495DB7%40AdobeOrg=1406116232%7CMCIDTS%7C17893%7CMCMID%7C82500003709226011980164123367731652803%7CMCAAMLH-1546512998%7C3%7CMCAAMB-1546512998%7CRKhpRz8krg2tLO6pguXWp5olkAcUniQYPHaMWWgdJ3xzPWQmdj0y%7CMCOPTOUT-1545915395s%7CNONE%7CMCAID%7C2E0BC192052A2F40-60000107E00320EA%7CvVersion%7C2.5.0; _ga=GA1.2.1968670231.1545908202; __stp={"visit":"returning","uuid":"f418c7d0-8f24-4f28-9f0a-8970aafc6596"}; QGUserId=%225918244913030327%22; __stdf=0; username=mf_cookie; mfutmkey=default; mfgid=; usernameFP=0qyy7EiXGbYBYdZqlhPjxi6AuPWrOA7IUYGsaFLezvfi817w6f1545908202756; cto_lwid=8a0bec89-ed05-47a7-9c7b-4c79911a4974; cto_idcpy=b1fe2046-14f5-43e0-81b8-25b0334afb95; s_sq=shoppersstopglobalprod%3D%2526c.%2526a.%2526activitymap.%2526page%253Dhome%25257Cwomen%25257Cindianwear%25257Csalwar%252520%252526%252520churidar%252520suits%25257Chaute%252520curry%252520womens%252520tie%252520up%252520neck%252520printed%252520churidar%252520suit%2526link%253DAdd%252520to%252520bag%2526region%253DaddToCartForm%2526pageIDType%253D1%2526.activitymap%2526.a%2526.c%2526pid%253Dhome%25257Cwomen%25257Cindianwear%25257Csalwar%252520%252526%252520churidar%252520suits%25257Chaute%252520curry%252520womens%252520tie%252520up%252520neck%252520printed%252520churidar%252520suit%2526pidt%253D1%2526oid%253DAdd%252520to%252520bag%2526oidt%253D3%2526ot%253DSUBMIT; ssl-cart=fd01a52b-fd6b-4f72-8418-21662632e5d4; _dy_c_att_exps=; AWSELB=D96111AB04A3BD85995A5C744AB89C62341F162313A2C7EFBF8D34DA5D430D94868A486BC2F98C3A8EB5B284DCD129892125FBF76526877FB8CEFB69EE1A647F4AEA570771; recentlyViewed=203566837|; s_ppn=home%7Cwomen%7Cindianwear%7Csalwar%20%26%20churidar%20suits%7Chaute%20curry%20womens%20tie%20up%20neck%20printed%20churidar%20suit; _gid=GA1.2.768515697.1546099756; mfutm=Uo9hyFmjUXEXeoTs5BBhLSkWzRCircHt2ZIVB5qWf4TzilLKca1546102647938; __sts={"sid":1546099756575,"tx":1546102602581,"url":"https%3A%2F%2Fwww.shoppersstop.com%2Fcart","pet":1546102602581,"set":1546099756575,"pUrl":"https%3A%2F%2Fwww.shoppersstop.com%2Fhaute-curry-womens-tie-up-neck-printed-churidar-suit%2Fp-203566837","pPet":1546102514522,"pTx":1546102514522}; __stgeo="0"; __stbpnenable=0; _qg_pushrequest=true',
'TE': 'Trailers'}

,请求正文如下

qty=1&baseProductCode=203566837&productCodePost=203566842&CSRFToken=d9ce7316-d5b4-4c46-93f1-449315652c36

我尝试使用邮递员通过将请求正文参数作为邮递员中的参数传递来进行api调用,并且该调用在自动添加到购物车中的项目上效果很好。但是,当我尝试使用python中的request.post(requests.post(url = url,headers= headers,params=params))进行相同的调用时,出现如下错误。标头是具有上述定义和参数的字典,我正以与上述请求正文完全相同的形式将其作为字符串传递。

  File "<ipython-input-20-6458ef84d465>", line 1, in <module>
    requests.post(url = url,headers= headers,params=params)

  File "c:\users\jithinjames\appdata\local\programs\python\python35\lib\site-packages\requests\api.py", line 112, in post
    return request('post', url, data=data, json=json, **kwargs)

  File "c:\users\jithinjames\appdata\local\programs\python\python35\lib\site-packages\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)

  File "c:\users\jithinjames\appdata\local\programs\python\python35\lib\site-packages\requests\sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)

  File "c:\users\jithinjames\appdata\local\programs\python\python35\lib\site-packages\requests\sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)

  File "c:\users\jithinjames\appdata\local\programs\python\python35\lib\site-packages\requests\adapters.py", line 495, in send
    raise ConnectionError(err, request=request)

ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

在这里您需要将数据作为字典发布到数​​据参数here 所以您需要这样:

data={
  'qty':1,
  'baseProductCode':'203566837',
  'productCodePost':'203566842',
  'CSRFToken':'d9ce7316-d5b4-4c46-93f1-449315652c36'
}
requests.post(url = url,headers= headers,params=params,data=data)