1、安装selenium
pip install selenium
2、打开浏览器登录获取cookie
from selenium import webdriver import time import json driver = webdriver.Chrome() # 创建Chrome对象. # 操作这个对象. driver.get('https://baidu.com') # get方式访问 time.sleep(5) el = driver.find_element_by_xpath("//input[@type='text']") el.send_keys('***********') time.sleep(2) el = driver.find_element_by_xpath("//input[@type='password']") el.send_keys('*****') time.sleep(2) el = driver.find_element_by_id("submit-login") el.click() time.sleep(2) #获取网站cookie diccookie=driver.get_cookies() fw=open('cookie.txt','w') json.dump(diccookie,fw) fw.close() #driver.get_screenshot_as_file('foo.png') time.sleep(10) #driver.quit() # 使用完, 记得关闭浏览器, 不然chromedriver.exe进程为一直在内存中.
3、读取使用cookie:
from selenium import webdriver import time import json driver = webdriver.Chrome() # 创建Chrome对象. driver.get('https://mp.qutoutiao.net') fr=open('cookie.txt','r') cookielist=json.load(fr) fr.close() for cookie in cookielist: driver.add_cookie(cookie) # 操作这个对象. driver.get('https://mp.qutoutiao.net') # get方式访问 time.sleep(1) #driver.get_screenshot_as_file('foo.png') #time.sleep(10) #driver.quit() # 使用完, 记得关闭浏览器, 不然chromedriver.exe进程为一直在内存中.
4、登录完成,开始干活
#-*-coding:utf-8-*- from selenium import webdriver import time import json driver = webdriver.Chrome() # 创建Chrome对象. driver.maximize_window() # 操作这个对象. driver.get('https://mp.***.net') # get方式访问 time.sleep(5) el = driver.find_element_by_xpath("//input[@type='text']") el.send_keys('****') time.sleep(2) el = driver.find_element_by_xpath("//input[@type='password']") el.send_keys('*****') time.sleep(2) el = driver.find_element_by_id("submit-login") el.click() time.sleep(2) #获取网站cookie diccookie=driver.get_cookies() fw=open('cookie.txt','w') json.dump(diccookie,fw) fw.close() el = driver.find_element_by_class_name("shadow-close") el.click() time.sleep(2) #滚动到浏览器顶部 js_top = "var q=document.documentElement.scrollTop=0" #滚动到浏览器底部 js_bottom = "var q=document.documentElement.scrollTop=10000" js_bottom2 = "window.scrollTo(0,document.body.scrollHeight)" driver.execute_script(js_top) el = driver.find_element_by_class_name("sub-child-txt") el.click() time.sleep(2) el = driver.find_element_by_xpath('//div[@class="content-header"]/div/p[3]') el.click() time.sleep(2) #article 图文1,3 video视频2,4 el = driver.find_element_by_xpath('//div[@class="content-article"]/div/p[4]') el.click() time.sleep(2) for i in range(1,100): try: el = driver.find_element_by_class_name("icon-delete") el.click() time.sleep(2) el = driver.find_element_by_class_name("el-button--primary") el.click() time.sleep(2) #driver.refresh() except: pass #print(txt) #driver.get_screenshot_as_file('foo.png') time.sleep(10) #driver.quit() # 使用完, 记得关闭浏览器, 不然chromedriver.exe进程为一直在内存中.