Python 如何打开请求会话?
我在这个问题上遇到了困难: 我必须使用请求执行一些任务(强制要求我使用这些任务的请求,因此我首先不能完全使用selenium),然后将我已进行的会话(使用requests.session())“转移”到selenium,并打开一个加载该会话的webdriver 我该怎么做 代码如下:Python 如何打开请求会话?,python,selenium,selenium-webdriver,python-requests,Python,Selenium,Selenium Webdriver,Python Requests,我在这个问题上遇到了困难: 我必须使用请求执行一些任务(强制要求我使用这些任务的请求,因此我首先不能完全使用selenium),然后将我已进行的会话(使用requests.session())“转移”到selenium,并打开一个加载该会话的webdriver 我该怎么做 代码如下: import requests import time from selenium import webdriver delay = 2 headers = { 'user-agent': 'Mozilla/
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
回溯(最近一次呼叫最后一次):
文件“\nigramercato.py”,第129行,在
最后一步()
文件“\nigramercato.py”,第112行,在最后一步中
对于会话中的c.cookies():
TypeError:“RequestScookeJar”对象不可调用
错误
会话中c的
cookies():
是一个错误,因为会话.cookies
是一个属性()
调用函数,但它不是函数。原始链接也没有()
。总之,应该是:对于会话中的c.cookies:
实际上,我正试图做与您提议完全相反的事情:我正在尝试将一个已经包含请求的会话转移到selenium。不是因为它。@Miopadrone好的。我将上面的链接修改为应该这样做的链接。同样,不幸的是,解决方案似乎不起作用。也许我做错了什么,但这就是我得到的TypeError:“requestScookejar”对象不可调用
@Miopadrone您必须提供您尝试的代码以及导致错误的原因。是的,当然。给你。我正在尝试自动化在网站上购买东西的过程。