爬虫学习-selenium模块

news/2024/7/19 11:30:39 标签: 爬虫, 学习, selenium
  • 可以便携的获取网站中动态加载的数据

  • 便携实现模拟登录

  • 基于游览器自动化的一个模块(按键精灵脚本)

    • 使用流程

  • 下载一个游览器的驱动程序:谷歌浏览器驱动安装 - 琳达的博客 - 博客园 (cnblogs.com)

  • 实例化一个游览器对象

  • 编写基于游览器自动化的操作代码

    • 一些自动化操作

  • 发起请求:get(url)

  • 标签定位:find系列的方法

  • 标签交互:send_keys('xxx')

  • 点击:click()

  • 执行js程序:execute_script('jsCode')

  • 前进、后退:back()、forward()

  • 关闭游览器: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()
    • 处理iframe

  • 如果定位的标签存在iframe标签内的,则必须使用switch_to.frame(id)

  • 动作链

  • from selenium.webdriver import ActionChains

  • action=ActionChains(bro):实例化动作链

  • action.click_and_hold(指定标签):点击长按指定的标签

  • action.move_by_offset(x,y).perform():偏移一定像素,x是水平方向,y是竖直方向

  • perfrom():立即执行动作链操作

  • action.release():释放动作链

  • 无可视化界面(无头游览器)

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)
  • 案例

  • 12306登录

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()


http://www.niftyadmin.cn/n/24159.html

相关文章

SAP入门技术分享二:数据类型

数据类型1.概要2.数据类型的种类(1)ABAP基本数据类型(2)局部数据类型(3)全局数据类型3.DATA语句(1)TYPE type(2)LIKE num(3)VALUE int…

redhat9---MySQL8练习

目录 1、找出部门30中的所有员工; 2、列出所有办事员(CLERK)的姓名,编号和部门编号; 3、找出奖金高于工资的员工; 4、找出奖金高于工资的60%的员工; 5、找出部门10中的所有经理(MANAGER)和部门20中所有的办事员(C…

【概率论】期末复习笔记:假设检验

假设检验目录一、假设检验的基本概念1. 假设检验的基本原理2. 两类错误3. 假设检验的一般步骤4. ppp值二、正态总体参数的假设检验σ2已知,检验μ与μ0的关系\color{dodgerblue}\sigma^2\text{已知,检验}\mu\text{与}\mu_0\text{的关系}σ2已知&#xff…

一、块级绑定

文章目录一、块级绑定1.1 var声明与变量提升1.2 块级声明1.2.1 let声明1.2.2 禁止重复声明1.2.3 常量声明1.2.4 使用const声明对象二、总结一、块级绑定 大多数类C语言中,变量(或绑定)总是在它被声明的地方创建。然而JS就不是这样,变量实际创建的位置取决…

《收获,不止Oracle》表的设计之五朵金花

表设计主要强调什么场合该选择什么技术,没有最高级的技术,只有最适合的技术。 1.表的特性 普通堆表的不足之处 1.查看产生多少日志 [oracleoracle-db-19c ~]$ sqlplus / as sysdbaSQL*Plus: Release 19.0.0.0.0 - Production on Wed Jan 4 14:27:13 20…

用Python让奇怪的想法变成现实,2023年继续创作

2023年继续写作,用文章记录生活 时间过得真快,一下就到2023年了。 由于疫情肆虐,在网络的游弋的实现也长了,写作的自然也多了。 回想一下,2018-2021年这三年时间里一篇文章也没写过为0,哈哈,没…

3D预处理的步骤(关于API,GPU的说明)

3D预处理的步骤(关于API,GPU的说明) CG在哪里适合? •视觉计算的各个方面 •更广泛使用计算机图形 CG是Computer Graphics IP是Image Processing(also image analysis) CV是Computer Vision(also pattern recognition) 图像可能是真实,合成,…

在华为云ecs中使用阿里云的yum源安装php7.4

在经历了编译安装的痛苦之后,还是决定使用yum安装了【可选】查询是否安装了软件,目的是卸载编译安装的phprpm -qa | grep -i 软件名rpm -qa | grep phprm -rf /usr/local/php修改yum下载源为阿里云的地址wget -O /etc/yum.repos.d/CentOS-Base.repo http…