微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 POST 请求将产品添加到购物车 - 无法添加到购物车

如何解决使用 POST 请求将产品添加到购物车 - 无法添加到购物车

我相信一年前有人问过类似的问题,但没有找到解决方案。基本上,我正在尝试使用 POST 请求将产品从 https://www.supremenewyork.com/shop/all 添加到购物车。我首先对 https://www.supremenewyork.com/mobile_stock.json 执行 GET 以检索项目名称和 ID,然后我使用该 ID 尝试发出 POST 请求,但它不起作用。

这是我的代码

# this is the get request
link = 'https://www.supremenewyork.com/mobile_stock.json'
response = requests.get(url=link)
data = json.loads(response.content.decode('utf-8'))
mylist = []
global mylists
mylists = mylist
for items in data['products_and_categories']:
    if items != 'new':
        categories = items
    for x in categories.split():
        for result in data['products_and_categories']['{}'.format(x)]:
            # finds the name and id
            if keyword in result['name'].lower():
                print('Product Found!')
                global name
                name = result['name']
                id = result['id']
                # attempts to make a POST request to add to cart
                post_headers = {'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/80.0.3987.132 Safari/537.36','x-requested-with': 'XMLHttpRequest','content-type': 'application/x-www-form-urlencoded'}
                post_data = {"utf8": "%E2%9C%93","authenticity_token": "{}","h": 1,"st": 29655,"chk": "1614789969ee693a8a188da014ffe17d","s": 86171}
                url = "https://www.supremenewyork.com/shop/{}/add".format(result['id'])
                add_to_cart = requests.post(url=url,headers=post_headers,data=post_data)
                print(id)
                print(add_to_cart.text)
                print(add_to_cart.status_code)
                print()

我得到的控制台错误"cart" = [],但状态代码200 (OK)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。