先登录拿到cookie保存到cookie.json文件,然后使用cookie登录进行操作
4掘金批量点赞.py
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
import json
# 获取配置对象 => 什么样的浏览器就选择什么浏览器配置
option = webdriver.ChromeOptions()
option.add_experimental_option("detach", True)
# 获取driver对象, 并将配置好的option传入进去
driver = webdriver.Chrome(options=option)
driver.get('https://juejin.cn/post/7286762580877901865')
# 等待2分钟
driver.implicitly_wait(2 * 60)
input("如果登录完成就请按回车键执行后续操作")
# 获取cookies
cookies = driver.get_cookies()
print(cookies)
jsonCookies = json.dumps(cookies)
with open("cookies.json", 'w') as f:
f.write(jsonCookies)
driver.quit()
# 使用XPath来获取元素
# element = driver.find_element(By.ID, "kw")
# element.send_keys("学习python可以做什么 ?")
# driver.implicitly_wait(0.5)
# element.send_keys(Keys.ENTER)
# element = driver.find_element(By.NAME,"wd")
# fruits = driver.find_element(By.ID, "fruits")
# fruit = fruits.find_element(By.CLASS_NAME, "tomatoes")
# 获取输入框的值
# print(element.click())
4.1掘金使用cookie登录
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
import json
# 获取配置对象 => 什么样的浏览器就选择什么浏览器配置
option = webdriver.ChromeOptions()
option.add_experimental_option("detach", True)
# 获取driver对象, 并将配置好的option传入进去
driver = webdriver.Chrome(options=option)
driver.get("https://juejin.cn/post/7218554163050758201")
driver.delete_all_cookies()
with open("cookies.json", 'r', encoding='utf-8') as f:
cookies = json.loads(f.read())
for cookie in cookies:
driver.add_cookie({'domain': '.juejin.cn',
'name': cookie['name'],
'value': cookie['value'],
'path': '/',
'expires': None})
driver.get("https://juejin.cn/post/7218554163050758201")
driver.implicitly_wait(3)
elements = driver.find_elements(By.CLASS_NAME, "action-digg")
for element in elements:
element.click()
# input()
# driver.quit()