该站点如何在POST请求上形成标题?

2024-04-25 07:01:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图了解当用户输入zipcode并从以下网站发出“POST”命令(通过单击“Shop Now”按钮)时,标题是如何构造的:

enter image description here

我相信这个“POST”请求中有趣的部分是站点如何形成以下标题,但我不知道它是如何做到的(我怀疑是有一些JavaScript/Angular代码造成的):

x-ccwfdfx7-a
x-ccwfdfx7-b
x-ccwfdfx7-c
x-ccwfdfx7-d
x-ccwfdfx7-f
x-ccwfdfx7-z

因此,我尝试使用requests模块以来宾身份登录,以了解有关此流如何工作的更多信息:

  1. 带请求的会话()
  2. 使用cloudscraper.create_scraper()

到目前为止,我所有的尝试都失败了。这是我的密码:

import requests  
from requests_toolbelt.utils import dump   #pip install requests_toolbelt
import cloudscraper   #pip install cloudscraper

#with requests.Session() as session:
with cloudscraper.create_scraper(
        browser={
            'custom': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
        }
    ) as session:

        CITY = XXXXX
        ZIPCODE = XXXXX

        #get cookies
        url = 'http://www.peapod.com'
        res1 = session.get(url)
        session.headers['Referer'] = 'https://www.peapod.com/'

        #get more cookies
        url = 'http://www.peapod.com/login'
        res2 = session.get(url)

        #get more cookies
        url = 'https://www.peapod.com/ppd/bundles/js/ppdBundle.js'
        res3 = session.get(url)

        #get all the service locations
        response = session.get('https://www.peapod.com/api/v4.0/serviceLocations',
            params={
                'customerType': 'C',
                'zip': ZIPCODE
            }
        )

        try:
            loc_id = list(
                filter(
                    lambda x: x.get('location', {}).get('city') == CITY, response.json()['response']['locations']
                )
            )[0]['location']['id']
        except IndexError:
            raise ValueError("Can't find City '{}' -> Zip {}".format(CITY, ZIPCODE))

        #login as guest
        response = session.post('https://www.peapod.com/api/v4.0/user/guest',
            json={
                'customerType': 'C',
                'cities': None,
                'email': None,
                'serviceLocationId': loc_id,
                'zip': ZIPCODE
            },
            params={
                'serviceLocationId': loc_id,
                'zip': ZIPCODE
            }
        )

这似乎产生了某种错误消息,说“我被阻止了”,我认为这是因为我无法理解浏览器如何在“POST”请求中构造ccwfdfx7头(我怀疑有一些JavaScript/Angular代码负责构建这些头文件,但我找不到它,希望有人能帮忙……)

在同一台计算机上,Chrome浏览器可以很好地登录


Tags: httpscomidurlgetresponsesessionwww