selenium-Chrome 操作cookie

selenium在应对需要登录验证的网页爬取上是个很好的工具,但是在登录过后拿到cookie,就可以直接使用cookie访问

以下是一个cookie登录的案例;目标网站:https://aso100.com/

导入相关库
<span>from</span> selenium <span>import</span> webdriver
<span>from</span> selenium.webdriver.support.ui <span>import</span> WebDriverWait
<span>from</span> selenium.webdriver.common.by <span>import</span> By
<span>from</span> selenium.webdriver.support <span>import</span> expected_conditions <span>as</span> EC
<span class="copy-code-btn">复制代码</span>
实现登录
userName = <span>'magic111'</span>
passWord = <span>'121314'</span>

driver = webdriver.Chrome()
driver.get(<span>'https://aso100.com/account/signin'</span>)
<span># 等待20秒直到访问成功</span>
WebDriverWait(driver, <span>20</span>).until(EC.presence_of_element_located((By.XPATH, <span>'//*[@id="username"]'</span>))) 
user_name = driver.find_element_by_xpath(<span>'//*[@id="username"]'</span>)
user_name.send_keys(userName)
pass_word = driver.find_element_by_xpath(<span>'//*[@id="password"]'</span>)
pass_word.send_keys(passWord)
submit = driver.find_element_by_xpath(<span>'//*[@id="submit"]'</span>)
submit.click()
<span># 等待20秒直到访问成功</span>
WebDriverWait(driver, <span>20</span>).until(EC.presence_of_element_located((By.XPATH, <span>'//*[@id="search-kw"]'</span>)))

<span># 获取cookies</span>
cookie_list = driver.get_cookies()
print(cookie_list)
driver.close()
driver.quit()
<span class="copy-code-btn">复制代码</span>
1、使用selenium设置cookie登录
driver = webdriver.Chrome()
<span># 要先访问一次这个域名</span>
driver.get(<span>'https://aso100.com'</span>)

<span>for</span> item <span>in</span> cookie_list: driver.add_cookie({
    <span>'domain'</span>: <span>'.aso100.com'</span>,
    <span>'name'</span>: item[<span>'name'</span>],
    <span>'value'</span>: item[<span>'value'</span>],
    <span>'path'</span>: <span>'/'</span>,
    <span>'expires'</span>: <span>None</span>
})

driver.get(<span>'https://aso100.com/account/setting/type/dataCenter'</span>)
input(<span>'是否有效'</span>)
driver.close()
driver.quit()
<span class="copy-code-btn">复制代码</span>
2、使用requests设置cookie登录
<span>import</span> requests
cookies = <span>";"</span>.join([item[<span>"name"</span>] + <span>"="</span> + item[<span>"value"</span>] + <span>""</span> <span>for</span> item <span>in</span> cookie_list])
print(cookies)
session = requests.Session()
<span># cookie要放到headers里</span>
headers = {
    <span>'Cookie'</span>: cookies
}
html = session.get(url=<span>'https://aso100.com/account/setting/type/dataCenter'</span>, headers=headers).content.decode()
print(html)
<span class="copy-code-btn">复制代码</span>

selenium-Chrome 操作cookie的相似文章

node puppeteer 爬虫,node puppeteer学习分析抖音xlog算法逆向,抖音 js 逆向算法,python分析python抖音爬虫分析python爬虫 微信公众号模拟登陆js解密分析python Selenium基础分析