栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python爬虫selenium的使用

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python爬虫selenium的使用

python爬虫selenium的使用
    • 简介
    • 环境搭建
    • selenium webdriver的使用
    • 实现12306拖拽登录验证实例
    • 利用超级鹰登录超级鹰网站图片验证实例

简介

—Selenium是最广泛使用的开源Web UI(用户界面)自动化测试套件之一。
----让我的程序连接到浏览器,让浏览器来完成各种复杂的操作,我们只接受最终的结果
-----selenium: 自动化测试工具
----可以打开浏览器。然后像人一样去操作浏览器, 程序员可以selenium中直接提取网页上的各种信息

环境搭建

pip install selenium -i清华源

----下载浏览器驱动; https://npm.taobao.org/mirrors/chromedriver
----把解压缩的浏览器驱动chromedriver 放在python解释器所在的文件夹
----让selenium启动谷歌浏览器

selenium webdriver的使用

----首先创建浏览器对象(以Chrome为例)
driver = Chrome()

  1. 获取网页,获取网页有两种方法:

----使用Get方法 -driver.get("www.yiibai.com");

----使用Navigate方法 -driver.navigate().to("https://yiibai.com/selenium/");

  1. 查找表单并发送用户输入
web.find_element_by_xpath('//[@id="JuserName"]').send_keys('***')
  1. 清除用户输入
    clear()方法用于从文本框中清除用户输入。
driver.find_element_by_xpath().clear();
  1. 通过Web元素获取数据
    有时需要获取通过web元素写入的文本来执行某些断言和调试。使用getText()方法来获取通过任何web元素写入的数据。
driver.find_element_by_xpath(**).getText();
  1. 执行Click事件
    click()方法用于对任何Web元素执行单击操作。
driver.find_element_by_xpath(**).click();
  1. 在浏览器历史记录中向后导航
driver.navigate().back();
  1. 在浏览器历史记录中向前导航
driver.navigate().forward();
  1. 刷新/重新加载网页
driver.navigate().refresh();
  1. 关闭浏览器
driver.close();
  1. 关闭浏览器和与驱动程序关联的其他所有其他窗口
driver.quit();
  1. 在Windows之间移动
driver.switch_to_window(web.window_handles[])

返回上一个窗口

web.switch_to_default_content()
  1. 在 frame 之间移动
web.switch_to_frame(**)
  1. 拖放
    使用Action类执行拖放操作。
WebElement element = driver.findElement(By.name("source"));  
WebElement target = driver.findElement(By.name("target"));  

14.无头浏览器的实现
opt = Options()

# opt.add_argument("--headless")
# opt.add_argument("--disable-gpu")
opt.add_argument('--disable-blink-features=AutomationControlled')

#1.创建浏览器对象
web = Chrome(options=opt)
实现12306拖拽登录验证实例

from selenium.webdriver.chrome.options import Options
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
import time

from selenium.webdriver.common.action_chains import ActionChains
opt = Options()
# opt.add_argument("--headless")
# opt.add_argument("--disable-gpu")
opt.add_argument('--disable-blink-features=AutomationControlled')

#1.创建浏览器对象
web = Chrome(options=opt)
# 2.打开一个网址
web.get("https://kyfw.12306.cn/otn/resources/login.html")
print(web.title)
web.find_element_by_xpath('//*[@id="toolbar_Div"]/div[2]/div[2]/ul/li[2]/a').click()
web.find_element_by_xpath('//*[@id="J-userName"]').send_keys('用户名')
web.find_element_by_xpath('//*[@id="J-password"]').send_keys('密码')
web.find_element_by_xpath('//*[@id="J-login"]').click()
time.sleep(3)
index = web.find_element_by_xpath('//*[@id="nc_1__scale_text"]/span')
ActionChains(web).drag_and_drop_by_offset(index, 300, 0).perform()


利用超级鹰登录超级鹰网站图片验证实例
from chaojiying import Chaojiying_Client

from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
web = Chrome()
web.get("https://www.chaojiying.com/user/login/")

image = web.find_element_by_xpath("/html/body/div[3]/div/div[3]/div[1]/form/div/img").screenshot_as_png

chaojiying = Chaojiying_Client('用户名', '密码', 'id')
print(chaojiying.PostPic(image, 1902))
dic = chaojiying.PostPic(image, 1902)
vs = dic['pic_str']

web.find_element_by_xpath("/html/body/div[3]/div/div[3]/div[1]/form/p[1]/input").send_keys('用户名')
web.find_element_by_xpath("/html/body/div[3]/div/div[3]/div[1]/form/p[2]/input").send_keys('密码')
web.find_element_by_xpath("/html/body/div[3]/div/div[3]/div[1]/form/p[3]/input").send_keys(vs,Keys.ENTER)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/286802.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号