目錄
獲得cookie
實(shí)現(xiàn)登陸
獲得cookie
首先在用戶登陸界面手動(dòng)獲取用selenium點(diǎn)擊等操作獲得cookie,并保存至txt文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-505676.html
def login():
executable_path = 'geckodriver'
driver = webdriver.Firefox(executable_path=executable_path)
driver.get('xxxxxx')
try:
driver.find_element_by_link_text('其他方式登錄').click()
xxxxxxx
time.sleep(20)
except Exception as exception:
print(f"ERROR !!! {exception}")
time.sleep(10)
cookies = driver.get_cookies()
with open('cookies.txt', 'w') as the_file:
for i in cookies:
the_file.write(str(i))
the_file.write('\n')
driver.close()
實(shí)現(xiàn)登陸
在新鏈接添加cookie實(shí)現(xiàn)用戶保持登陸文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-505676.html
def get_html(url):
"""獲取網(wǎng)頁(yè)源代碼,返回json格式."""
headers = None
executable_path = 'geckodriver'
driver = webdriver.Firefox(executable_path=executable_path)
driver.get(f'xxxxxx')
cookies = []
with open('cookies.txt', 'r') as the_file:
for i_file in the_file:
cookies.append(eval(i_file.strip()))
for cookie in cookies:
driver.add_cookie(cookie)
driver.refresh()
time.sleep(2)
到了這里,關(guān)于selenium保持用戶登陸的方法(獲取cookie和添加cookie)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!