selenium把cookies傳遞給requests


from selenium  import  webdriver
from lxml  import  etree
import  requests



driver=webdriver.Chrome()
driver.get("https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx")
賬號=driver.find_element_by_xpath('//*[@id="email"]')
賬號.send_keys("xxxxx")
密碼=driver.find_element_by_xpath('//*[@id="pwd"]')
密碼.send_keys("lhxxx")
驗證碼=driver.find_element_by_xpath('//*[@id="code"]')
a=input("請輸入驗證碼:")
驗證碼.send_keys(a)
登錄=driver.find_element_by_xpath('//*[@id="denglu"]')
登錄.click()
headers={"user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36"}
#獲取當前網址
url=driver.current_url
#獲取cookies
cookies_1=driver.get_cookies()
cookies={}
for i in cookies_1:
    cookies[i["name"]]=i["value"]
print(cookies)
response=requests.get(url,headers=headers,cookies=cookies).text
content=etree.HTML(response)
c=content.xpath('//*[@id="mainSearch"]/div[3]/div[1]/div[1]/a/text()')[0]
print(c)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM