【问题标题】:How to open a requests session?如何打开请求会话?
【发布时间】:2020-03-13 03:40:48
【问题描述】:

我很难解决这个问题: 我必须使用请求执行一些任务(我必须对这些任务使用请求,所以我不能一开始就使用硒),然后“转移”我已经进行的会话(使用 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

【问题讨论】:

    标签: python selenium selenium-webdriver python-requests


    【解决方案1】:

    python-requests cookies export to selenium

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

    【讨论】:

    • 实际上,我正在尝试做与您建议完全相反的事情:我正在尝试将已经带有请求的会话转移到 selenium。不是来自它。
    • @Miopadrone 好的。我将上面的链接修改为应该这样做的链接。
    • 再次遗憾的是,该解决方案似乎不起作用。也许我做错了什么,但这就是我得到的TypeError: 'RequestsCookieJar' object is not callable
    • @Miopadrone 您将不得不提供您尝试过的代码以及出现错误的原因。
    • 是的,当然。这里是。我正在尝试自动化在网站上购买东西的过程。
    猜你喜欢
    • 2015-11-28
    • 2019-02-28
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 2012-05-26
    相关资源
    最近更新 更多