Python3.x无法在footlocker上添加项目到购物车

时间:2017-01-06 11:03:23

标签: python python-3.x web-crawler

我试着在footlocker.com上写一只蜘蛛来买鞋子。当我将项目添加到购物车时,我将正确的数据发布到服务器,但我收到了:

“商品无法添加到购物车:CloseX 如果你再试一次,你可以抓住一对。由于需求量大,释放鞋过滤到现场以避免任何问题。“

我仔细地将请求和数据与浏览器进行了比较,但我没有发现任何差异。

以下是代码:

def __init__(self):
    print('Initialize')

    # Log in data
    self.login_email = self.login_password = ''
    self.session = ''

    # Product data
    self.product_url = ''
    self.model = self.sku = ''
    self.requestKey = ''
    self.model_name = self.selectedPrice = self.qty = self.size = ''

    self.header = {
        'User-Agent':
            'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 '
            '(KHTML, like Gecko) Chrome/57.0.2950.4 Safari/537.36'
    }

def set_info(self, login_email, login_password, product_url, qty, size):
    self.login_email = login_email
    self.login_password = login_password
    self.product_url = product_url
    self.qty = qty
    self.size = size

def login(self):
    print('Log in ...')

    # Open log in dialog and get request key
    # --------------------------------------------------------------------------------------------------------------
    login_page_url = 'https://www.footlocker.com/account/default.cfm'

    self.session = requests.session()
    response = self.session.get(login_page_url)
    request_key_pattern = re.compile('id=\"requestKey\" value=\"(\w{16})\"')
    self.requestKey = request_key_pattern.findall(response.text)[0]
    # --------------------------------------------------------------------------------------------------------------

    # Log in
    # --------------------------------------------------------------------------------------------------------------
    login_post_url = 'https://www.footlocker.com/account/default.cfm?action=accountSignIn'

    params = {
        'companyCode': '21',
        'requestKey': self.requestKey,
        'email': self.login_email,
        'password': self.login_password
    }

    response = self.session.post(login_post_url, data=params)

    if response.status_code == 200:
        print('200: Log in successfully')
    else:
        print(str(response.status_code) + ': Log in failed')
        sys.exit(1)
    # --------------------------------------------------------------------------------------------------------------

def set_product_info(self):
    print('Extract product information ...')

    # Regular expression
    model_pattern = re.compile('model:(\d{6})')
    sku_pattern = re.compile('sku:(\d{8})')
    request_key_pattern = re.compile('value=\"(\w{16})\" name=\"requestKey\"')
    model_name_pattern = re.compile('name=\"model_name\" value=\"([\w ]*-[\w \']*)\"')
    selected_price_pattern = re.compile('name=\"selectedPrice\" value=\"(\$\d*.\d*)\"')

    response = self.session.get(self.product_url)

    if response.status_code == 200:
        self.model = model_pattern.findall(self.product_url)[0]
        self.sku = sku_pattern.findall(self.product_url)[0]
        self.requestKey = request_key_pattern.findall(response.text)[0]
        self.model_name = model_name_pattern.findall(response.text)[0]
        self.selectedPrice = selected_price_pattern.findall(response.text)[0]
    else:
        print(response.status_code + ': Failed to open the product page')
        sys.exit(2)

def add_to_cart(self):
    print('Add item into the cart ...')

    cart_url = 'http://www.footlocker.com/catalog/miniAddToCart.cfm?secure=0&'

    params = {
        'storeCostOfGoods': '0.00',
        'lineItemId': '',
        'model': self.model,
        'requestKey': self.requestKey,
        'hasXYPromo': 'false',
        'sameDayDeliveryConfig': 'false',
        'sku': self.sku,
        'the_model_nbr': self.model,
        'model_name': self.model_name,
        'skipISA': 'no',
        'selectedPrice': self.selectedPrice,
        'qty': self.qty,
        'size': self.size,
        'fulfillmentType': 'SHIP_TO_HOME',
        'storeNumber': '00000',
        'coreMetricsDo': 'true',
        'coreMetricsCategory': 'Add to Wish List - PDP',
        'quantity': self.qty,
        'inlineAddToCart': '1'
    }

    response = self.session.post(cart_url, data=params)

    file = open('response.html', 'w')
    file.write(response.text)

    if response.status_code == 200:
        print('Item is added to the cart')
    else:
        print('Failed to add item')
        sys.exit(3)

感谢您的帮助。

0 个答案:

没有答案