我正在尝试使用Selenium登录Gmail,然后使用请求(或aiohttp)进行Google搜索。
在此之前,我使用纯硒登录并进行搜索,观看YouTube等。但是最近我开始问我是否可以使用纯请求登录Gmail。有人告诉我,由于使用了大量JavaScript,这非常困难。因此,我想到了一种新方法(使用Selenium登录然后继续使用请求),并想知道它是否可行。
driver = webdriver.Chrome()
driver.get("https://accounts.google.com/signin")
email_phone = driver.find_element_by_xpath("//input[@id='identifierId']")
email_phone.send_keys("your_emailid_phone")
driver.find_element_by_id("identifierNext").click()
password = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH, "//input[@name='password']")))
password.send_keys("your_password")
driver.find_element_by_id("passwordNext").click()
time.sleep(5)
driver.get("https://google.com")
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
session = requests.session()
with open('cookies.pkl', 'rb') as f:
session.cookies.update(pickle.load(f))
session.get("https://youtube.com")
运行此代码时没有错误。但是,我担心这种方法是否行得通。如果我加载使用Selenium登录Gmail时在那里的cookie,我的任何活动都会反映在我的Gmail中(我的意图)。
答案 0 :(得分:1)
如果您坚持使用selenium
,那么您应该知道
password
不应为WebDriverWait(driver, ...
。在这种情况下,WebDriverWait(driver, ...
对您没有任何帮助。当在时间限制内找不到特定元素时,也会引发错误。driver.get_cookies()
,您将无法获取属于https://myaccount.google.com/
的cookie 完整代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import pickle
def save_cookies():
email = "@gmail.com"
password = ""
driver = webdriver.Chrome()
driver.get("https://accounts.google.com/signin")
email_phone = driver.find_element_by_xpath("//input[@id='identifierId']")
email_phone.send_keys(email)
driver.find_element_by_id("identifierNext").click()
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='password']")))
password = driver.find_element_by_xpath("//input[@name='password']")
password.send_keys(password)
driver.find_element_by_id("passwordNext").click()
time.sleep(5)
google_cookies = driver.get_cookies()
cookies = ({cookie.get("name"):cookie.get("value") for cookie in google_cookies}, google_cookies[0].get("expiry"))
with open("cookies.pkl","wb") as fd:
pickle.dump(cookies, fd)
return cookies
import requests
with requests.Session() as s:
with open("cookies.pkl","rb") as fd:
cookies, expiry = pickle.load(fd)
if expiry < time.time():
cookies, expiry = save_cookies()
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Referer": "https://www.google.com/",
"Accept-Encoding": "deflate",
"Accept-Language": "en;q=0.6",
}
resp = s.get("https://myaccount.google.com/"
,headers=headers,cookies=cookies)
print(resp.url)