洛阳网站建设外包seo优化6个实用技巧
一、selenium简要说明
selenium是基于浏览器自动化的一个模块,它能便捷的获取网站中动态加载的数据,和实现模拟登录、爬虫等操作
二、实现流程
2.1 selenium前置操作
1. 安装selenium模块
pip3 install selenium
2. 下载浏览器内核程序
注意:驱动版本要和浏览器版本匹配,否则无法加载显示
谷歌驱动下载路径:chromedriver.storage.googleapis.com/index.html
Edge浏览器驱动下载: Microsoft Edge WebDriver - Microsoft Edge Developer
2.2 selenium内部基础流程
基础流程: 1. 发起请求: get() 2. 标签定位: find_element() 3. 标签交互: send_keys() 4. 执行js脚本: excute_script() 5. 前进、后退: back(),forward() 6. 关闭浏览器: quit()
2.3 selenium操作iframe标签
2.1 action = ActionChains(brower)
2.2 click_and_hold(div): 点击且长按
2.3 move_by_offset(x,y): 移动
2.4 perform(): 让动作链执行
2.5 action.release() :动作链释放
三、代码
'''
QQ空间登录
'''from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleepif __name__ == '__main__':#用户名和密码username = '45xxxxxxx'password = 'xxxxxxxx'#定义浏览器对象browser = webdriver.Edge(executable_path='./msedgedriver.exe')#发起请求browser.get('https://qzone.qq.com/')#定位标签#点击用户名密码登录,注意用户名密码登录在一个frame中browser.switch_to.frame('login_frame')user_password = browser.find_element(By.ID,'switcher_plogin')user_password.click()sleep(2)input_username = browser.find_element(By.ID,'u')input_passwd = browser.find_element(By.ID,'p')button = browser.find_element(By.ID,'login_button')#清空默认input_username.clear()input_passwd.clear()#输入用户名和密码input_username.send_keys(username)input_passwd.send_keys(password)sleep(2)#点击button.click()sleep(10)browser.quit()