有很多時候,爬取的頁面需要cookie來維持會話,但是其登入這塊反爬又很驗證,個人採用了這種手工登入獲取cookie的方式來獲取cookie,用的很多,於是寫成了乙個模板.
#!d:\python\python36 python
# encoding: utf-8
"""@author: sy_dove
@contact: sydove08152@gmail.com
@file: get_cookie.py
@time: 2019/6/8 10:57
@desc:
"""from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.by import by
from selenium.common.exceptions import timeoutexception
from selenium.webdriver.support import expected_conditions as ec
class
getcookie
(object):
def__init__
(self, url, is_driver)
: self.url = url
self.is_driver = is_driver
defrun(self)
: driver = webdriver.chrome(
) driver.get(url=self.url)
driver.maximize_window(
)# 設定等待,等待到已經手工完成登入
getcookie.is_visible(
'//a[text()="我的訂單"]'
, driver)
driver.refresh(
) cookies = driver.get_cookies(
)# 判斷cookie是否是給webdriver使用或是requests使用
if self.is_driver:
return cookies
else
: the_cookies =
# 獲取cookie中的name和value,轉化成requests可以使用的形式
for cookie in cookies:
the_cookies[cookie[
'name']]
= cookie[
'value'
]return the_cookies
@staticmethod
defis_visible
(locator, driver)
:"""設定顯示等待,等待到登入頭像出現"""
try:
ui.webdriverwait(driver,80)
.until(ec.visibility_of_element_located(
(by.xpath, locator)))
return
true
except timeoutexception:
return
false
defmain
(url)
: g1 = getcookie(url=url, is_driver=
true
) g1.run(
)if __name__ ==
'__main__'
: main(url=
'')#!d:\python\python36 python
# encoding: utf-8
"""@author: sy_dove
@contact: sydove08152@gmail.com
@file: get_cookie.py
@time: 2019/6/8 10:57
@desc:
"""from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.by import by
from selenium.common.exceptions import timeoutexception
from selenium.webdriver.support import expected_conditions as ec
class
getcookie
(object):
def__init__
(self, url, is_driver)
: self.url = url
self.is_driver = is_driver
defrun(self)
: driver = webdriver.chrome(
) driver.get(url=self.url)
driver.maximize_window(
)# 設定等待,等待到已經手工完成登入
getcookie.is_visible(
'//a[text()="我的訂單"]'
, driver)
driver.refresh(
) cookies = driver.get_cookies(
)# 判斷cookie是否是給webdriver使用或是requests使用
if self.is_driver:
return cookies
else
: the_cookies =
# 獲取cookie中的name和value,轉化成requests可以使用的形式
for cookie in cookies:
the_cookies[cookie[
'name']]
= cookie[
'value'
]return the_cookies
@staticmethod
defis_visible
(locator, driver)
:"""設定顯示等待,等待到登入頭像出現"""
try:
ui.webdriverwait(driver,80)
.until(ec.visibility_of_element_located(
(by.xpath, locator)))
return
true
except timeoutexception:
return
false
defmain
(url)
: g1 = getcookie(url=url, is_driver=
true
) g1.run(
)if __name__ ==
'__main__'
: main(url=
'')
javascript 設定和獲取cookie
在做專案的時候需要在前端獲取和設定cookie,原本想用jquery.cookie.js,但是懶得引入指令碼,就在js檔案裡直接使用了js的函式來呼叫。function getcookie cname return 呼叫 getcookie name function setcookie c nam...
js中設定cookie以及獲取cookie
當使用者訪問 web 頁面時,他的名字可以記錄在 cookie 中。在使用者下一次訪問該頁面時,可以在 cookie 中讀取使用者訪問記錄。cookie 以名 值對形式儲存 js中可以通過document.cookie屬性來建立,讀取以及刪除cookie document.cookie userna...
Selenium獲取元素資訊
一 為什麼要學習獲取元素資訊的方法?如何獲取元素的文字?如何獲取元素屬性值?如何讓程式判斷元素是否為可見狀態?要想解決以上問題,需要學習selenium封裝的獲取元素資訊的方法。二 獲取元素資訊的常用方法 size 返回元素大小 text 獲取元素的文字 get attribute 獲取屬性值,傳遞...