首页 > 解决方案 > 如何打开请求会话?

问题描述

我在这个问题上遇到了困难:我必须使用请求执行一些任务(我必须对这些任务使用请求,所以我不能一开始就使用硒)然后“转移”会话我已经(使用 requests.session() )继续使用 selenium 并打开加载了该会话的 webdriver。

我怎么做?

这是代码:

import requests
import time
from selenium import webdriver

delay = 2
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'
}

session = requests.Session()
session.headers.update(headers)


driver = webdriver.Chrome()
driver.get('https://www.nigramercato.com')

payloadcartupdate = {
  'token': '1eab12d3844d5201e62ff74588607f52',
  'id_product': '5089',
  'id_customization': '0',
  'group[1]': '28',
  'qty': '1',
  'add': '1',
  'action': 'update'
}

payloadajax = {
  'id_product_attribute': '29465',
  'id_product': '5039',
  'action': 'add-to-cart'
}

payloadorder = {
  'ajax_request': '1',
  'action': 'getShippingAndPaymentBlocks',
  'token': '1eab12d3844d5201e62ff74588607f52'
}

payloaddelivery = {
  'delivery_option[0]': '48',
  'selectDeliveryOption': '',
  'ajax_request': '1',
  'action': 'selectDeliveryOption',
  'token': '1eab12d3844d5201e62ff74588607f52'
}

payloadpayment = {
  'optionId': 'payment-option-2',
  'payment_fee': '0',
  'ajax_request': '1',
  'action': 'selectPaymentOption',
  'token': '1eab12d3844d5201e62ff74588607f52'
}

def visit_page():
  print('Visiting the page...')
  visitpage = session.get('https://nigramercato.com/en/jordan/5039-29465-AIR-JORDAN-1-RETRO-HIGH-OG-555088-060.html')
  print(f"Successfully visited the page, waiting the delay ({delay} seconds)...")
  print(f"Status code of the operation: {visitpage.status_code}")



def ajax_add_to_cart():
  print('Adding to cart...')
  ajaxaddtocart = session.post('https://nigramercato.com/en/module/ps_shoppingcart/ajax', data=payloadajax)
  print(f'Successfully added to cart, waiting the delay ({delay} seconds)...')
  print(f'Status code of the operation: {ajaxaddtocart.status_code}')
  time.sleep(delay)

def update_cart():
  print('Updating cart...')
  updatecart = session.post('https://nigramercato.com/en/cart', data=payloadcartupdate)
  print(f'Successfully updated cart, waiting the delay ({delay} seconds)...')
  time.sleep(delay)

def final_step():
  driver.get('https://nigramercato.com/en/order')
  cookiecount = 0
  for c in session.cookies():
    driver.add_cookie({
      'name': c.name,
      'value': c.value
    })
    cookiecount += 1
    print(f"Successfully imported {cookiecount} cookies")




visit_page()
ajax_add_to_cart()
update_cart()
final_step()

这是我得到的错误:

Traceback (most recent call last):
  File ".\nigramercato.py", line 129, in <module>
    final_step()
  File ".\nigramercato.py", line 112, in final_step
    for c in session.cookies():
TypeError: 'RequestsCookieJar' object is not callable

标签: pythonseleniumselenium-webdriverpython-requests

解决方案


python-requests cookie 导出到 selenium

错误 for c in session.cookies():是一个错误,因为session.cookies它是一个属性。()调用函数,但它不是函数。原来的链接也没有()。总之,应该是:for c in session.cookies:


推荐阅读