首页 > 解决方案 > 使用请求将商品添加到购物车 (Python)

问题描述

尝试将商品添加到购物车并打印购物车 cookie。已经在一些没有问题的网站上做到了这一点。但是这个站点上的请求有效负载看起来不同。链接到试图添加的项目 ->

https://www.footish.se/sneakers/nike-wmns-m2k-tekno-ao3108-104bq3378-002

有效载荷看起来像这样

{"item":{"__type":"JetShop.StoreControls.Services.AddToCartItem","Quantity":"1","AttributeData":["EUR 37.5","","MjQwMzA4fDEwOTkuMDB8LTEuMDB8QlEzMzc4LTAwMi0zNy41fHw8c3BhbiBjbGFzcz0iTWFza2VkSW5TdG9ja0Nzc0NsYXNzIj48c3Bhbj5JIGxhZ2VyPC9zcGFuPjwvc3Bhbj58VHJ1ZXwxfDE=",""],"ProductID":"37127","SpecificationIdList":"","Comments":[],"DiscountId":null},"culture":"sv-SE"

这就是我到目前为止所做的。

import requests
s = requests.Session()
payload = {"ProductID": "37127", "Quantity": "1"}
get = s.get("https://www.footish.se/sneakers/fila-wmns-disruptor-slide-1010868-1fg")
post = s.post("https://www.footish.se/Services/General.asmx/AddCartItem", data=payload)
print(post.status_code, post.content)
print(get.status_code, get.text)

尝试添加后,我得到一个状态代码“500”,它告诉我“AddCartItem Web 服务方法名称无效”。

关于如何添加项目的任何想法?

标签: pythonpython-requests

解决方案


上面代码的问题是由于您的代码发送的 Content-Type 。当我们检查上面的链接网络时,它显示了类似下面的片段。

:authority: www.footish.se
:method: POST
:path: /Services/General.asmx/AddCartItem
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7,ko;q=0.6
content-length: 362
content-type: application/json; charset=UTF-8
cookie: ASP.NET_SessionId=5nq3ssbdtc1xfkeyu0ugk2jo; JetShop_CartID=6f46a87d-d445-4149-b0f0-8ccea54ee2a8; SERVERID=cl03n04; _ga=GA1.2.973188721.1588161632; _gid=GA1.2.2048936945.1588161632; country_code=SE; RWuid=1588161634615146663; DV_TRACK=9d70aaa2-6a0c-4e56-88d6-3e86623f6c60; 2c.cId=5ea96c6960b2ac677e09f92b; RwServerDebug=false; _pk_ses.438.8ea7=1; JetShop_NoOfItemsInCart=1; __atuvc=3%7C18; __atuvs=5ea96c5ad286a611002; RWviewTrail=37127%2C37127; jibber-367-visitor-token=eyJ1c2VyX2lkIjoiNDc5MjYyOSIsInR5cGUiOiJjdXN0b21lciJ9.EYr-Gw.jNHbjoGTy97kTcBI5L_-L0WOlBA; _pk_id.438.8ea7=66eea615a6981820.1588161644.1.1588161693.1588161644.
origin: https://www.footish.se
referer: https://www.footish.se/sneakers/nike-wmns-m2k-tekno-ao3108-104bq3378-002
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36
x-requested-with: XMLHttpRequest

content-type: application/json; charset=UTF-8如果您通过添加以下内容来比较从代码发送的请求,该网站将使用where:

print(post.request.headers)

输出将是这样的。

{'Content-Length': '381', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.18.4', 'Connection': 'keep-alive', 'Cookie': 'SERVERID=cl03n07; JetShop_CartID=51205d52-9a79-4e35-aa7a-b1cae741fabd; ASP.NET_SessionId=urykwfhot2xdccp0suzk3qst', 'Content-Type': 'application/json'}

要解决此问题,您需要修改发送到服务器的标头和数据。

  • 使用 json 代替数据
import requests
s = requests.Session()
payload = {"item":{"__type":"JetShop.StoreControls.Services.AddToCartItem","Quantity":"1","AttributeData":["EUR 37.5","","MjQwMzA4fDEwOTkuMDB8LTEuMDB8QlEzMzc4LTAwMi0zNy41fHw8c3BhbiBjbGFzcz0iTWFza2VkSW5TdG9ja0Nzc0NsYXNzIj48c3Bhbj5JIGxhZ2VyPC9zcGFuPjwvc3Bhbj58VHJ1ZXwxfDE=",""],"ProductID":"37127","SpecificationIdList":"","Comments":[],"DiscountId":None},"culture":"sv-SE"}
get = s.get("https://www.footish.se/sneakers/fila-wmns-disruptor-slide-1010868-1fg")
post = s.post("https://www.footish.se/Services/General.asmx/AddCartItem", json=payload)
print(post.request.headers)
print(post.request)
print(post.text)
print(post.status_code)

或者

  • 定义标题并'Content-Type: application/json'使用添加并将dict转换为jsonjson.dumps(payload)
import json
import requests
s = requests.Session()
headers = {"Content-Type": "application/json"}
payload = {"item":{"__type":"JetShop.StoreControls.Services.AddToCartItem","Quantity":"1","AttributeData":["EUR 37.5","","MjQwMzA4fDEwOTkuMDB8LTEuMDB8QlEzMzc4LTAwMi0zNy41fHw8c3BhbiBjbGFzcz0iTWFza2VkSW5TdG9ja0Nzc0NsYXNzIj48c3Bhbj5JIGxhZ2VyPC9zcGFuPjwvc3Bhbj58VHJ1ZXwxfDE=",""],"ProductID":"37127","SpecificationIdList":"","Comments":[],"DiscountId":None},"culture":"sv-SE"}
get = s.get("https://www.footish.se/sneakers/fila-wmns-disruptor-slide-1010868-1fg")
post = s.post("https://www.footish.se/Services/General.asmx/AddCartItem", data=json.dumps(payload), headers=headers)
print(post.request.headers)
print(post.request)
print(post.text)
print(post.status_code)

推荐阅读