- Association with crawlers
Portable access to dynamically loaded data from websites
Portable implementation of simulated login
A module based on browser automation (key wizard script)
- manual
Environment installation pip install selenium
Download a browser driver: Google Chrome driver installation - Linda's Blog - Blog Park (cnblogs.com)
from selenium import webdriver
Instantiate a browser object
Write operation code based on browser automation
- some automated operations
Initiate a request: get(url)
Tag positioning: find series method
Tag interaction: send_keys('xxx')
Click: click()
Execute js program: execute_script('jsCode')
Forward, backward: back(), forward()
Close the browser: quit()
from selenium import webdriver
from lxml import etree
import time
# 实例化一个游览器对象
bro = webdriver.Chrome(executable_path='chromedriver.exe')
# 让游览器发起一个指定url对应请求
bro.get('https://i.qq.com/')
# 切换作用域
bro.switch_to.frame('login_frame')
a_tag=bro.find_element_by_id('switcher_plogin')
a_tag.click()
userName_tag=bro.find_element_by_id('u')
passWord_tag=bro.find_element_by_id('p')
time.sleep(3)
userName_tag.send_keys('2371964121')
time.sleep(3)
passWord_tag.send_keys('xxxxxx')
time.sleep(3)
btn=bro.find_element_by_id('login_button')
btn.click()
time.sleep(3)
bro.quit()
- Handle iframe
If the positioned tag exists within an iframe tag, switch_to.frame(id) must be used
action chain
from selenium.webdriver import ActionChains
action=ActionChains(bro): instantiate action chain
action.click_and_hold (specified label): Click and long press the specified label
action.move_by_offset(x, y).perform(): offset by a certain pixel, x is the horizontal direction, y is the vertical direction
perfrom(): Immediately execute the action chain operation
action.release(): Release action chain
No visual interface (headless browser)
from selenium import webdriver
# 无可视化界面操作
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('headless')
chrome_options.add_argument('disable-gpu')
# 实现让selenium规避被检测到的风险
from selenium.webdriver import ChromeOptions
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
bro = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=chrome_options, options=option)
bro.get('https://www.baidu.com/')
print(bro.page_source)
Case
12306 login
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains # 动作链
# 实现规避检测
from selenium.webdriver import ChromeOptions
import time
def login():
driver.find_element(By.ID, 'J-userName').send_keys('zh')
driver.find_element(By.ID, 'J-password').send_keys('mm')
driver.find_element(By.ID, 'J-login').click()
time.sleep(2)
# 滑动模块
clock = driver.find_element(By.CLASS_NAME, 'nc_iconfont')
action = ActionChains(driver)
# 点击长按滑动模块
action.click_and_hold(clock).perform()
for i in range(5):
action.move_by_offset(60, 0)
time.sleep(0.1)
action.release().perform()
if __name__ == '__main__':
url = 'https://kyfw.12306.cn/otn/resources/login.html'
options = ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option('excludeSwitches', ['enable-automation'])
driver = webdriver.Chrome(executable_path='./chromedriver.exe', options=options)
# 设置浏览器,防止selenium被检测出来
driver.get(url)
login()