Since the HTTP protocol is a stateless protocol, when the server communicates with the client through the http protocol, the server cannot record the client's information.
In order to solve this problem, it is realized through Cookie and Session technology.
Cookie attributes
The attributes of general cookies include:
Domain: Domain, which indicates which domain or subdomain the current cookie belongs to.
For the Set-Cookie returned by the server, if the Domain value is not specified, then the Domain value defaults to the main domain name corresponding to the currently submitted http request. For example, when visiting http://www.example.com and returning a cookie without a named domain value, it will be the default www.example.com.
Path: Indicates the path of the cookie.
Expire time/Max-age: indicates the validity period of the cookie. The value of expire is a time, after this time, the cookie becomes invalid. Or use max-age to specify the time after which the current cookie expires. If a cookie returned by the server does not specify its expire time, it means that the validity period of the cookie is only the current session, that is, the session cookie. After the current session ends, it expires. Correspondingly, when the page is closed (in the browser), the cookie should be deleted by the browser.
secure: indicates that the cookie can only be transmitted using https. Generally used for cookies containing authentication information. When requesting to transmit this cookie, it must be transmitted using https.
httponly: Indicates that this cookie must be used for http or https transmission. This means that browser scripts, such as javascript, are not allowed to access and manipulate this cookie.
Get cookie
# Log in to your Baidu account from the Baidu homepage and get cookies
from selenium import webdriver
from time import sleep
import json
# Launch chome browser and visit Baidu homepage
option=webdriver.ChromeOptions()
option.add_argument('--start-maximized')
drive=webdriver.Chrome(options=option)
drive.get('http://www.baidu.com')print(drive.title)
# Click to Login"
eled=drive.find_element_by_link_text("log in")
eled.click()sleep(2)
# Click "Username Login"
namelogin=drive.find_element_by_css_selector('p.tang-pass-footerBarULogin')
namelogin.click()sleep(2)
# Enter username, password and log in
username = drive.find_element_by_id('TANGRAM__PSP_10__userName')
username.send_keys('********')sleep(1)
password = drive.find_element_by_id('TANGRAM__PSP_10__password')
password.send_keys('********')sleep(1)
submit = drive.find_element_by_id('TANGRAM__PSP_10__submit')
submit.click()sleep(3)
# Get website cookies
diccookie=drive.get_cookies()
fw=open('baiducookie.txt','w')
json.dump(diccookie,fw)
fw.close()
Read the cookie from the hard disk and add it to the request sending server.
import time
import json
Launch chome browser and visit Baidu homepage
option=webdriver.ChromeOptions()
option.add_argument('--start-maximized')
drive=webdriver.Chrome(options=option)
drive.get('http://www.baidu.com')print(drive.title)
fr=open('baidu_cookie.txt','r')
cookielist=json.load(fr)
fr.close()for cookie in cookielist:
drive.add_cookie(cookie)sleep(5)
drive.get('http://www.baidu.com')
drive.refresh()
Failed to log in to Baidu through cookie:
Message: unable to set cookie
Message: invalid argument: invalid ‘expiry’
So far, this article on the implementation of python selenium operation cookie is introduced. For more related python selenium operation cookie content, please search for the previous article of ZaLou.Cn or continue to browse the related articles below. Hope you will support ZaLou.Cn more in the future. !
Recommended Posts