代码:
from bs4 import BeautifulSoup
import requests
#GET SESSION
s = requests.Session()
s.get('https://www.clos19.com/en-gb/')
#GET CSRFToken
r = requests.get('https://www.clos19.com/en-gb/login')
soup = BeautifulSoup(r.text, 'html.parser')
CSRFToken = soup.find('input', type='hidden')["value"]
print(soup.find('input', type='hidden')["value"])
#ADD TO CART
payload = {
'productCode': '100559',
'qty': '1',
'CSRFToken': CSRFToken
}
r = s.post('https://www.clos19.com/en-gb/ajax/addToCart', data=payload, allow_redirects=False)
print(r.status_code)
print(s.cookies.get_dict())
端子输出:
{'JSESSIONID': '448858956754C2F8F2DCF1CC4B803833', 'ROUTEID': '.app1', 'mhPreferredCountry': 'GB', 'mhPreferredLanguage': 'en_GB'}
如何仅打印JSESSIONID?因此,所需的打印值为448858956754C2F8F2DCF1CC4B803833
答案 0 :(得分:2)
将最后一行更改为
print(s.cookies['JSESSIONID'])