python爬虫入门(4)----- selenium
selenium
简介
selenium使用JavaScript模拟真实用户对浏览器进行操作。测试脚本执行时,浏览器自动按照脚本代码做出点击,输入,打开,验证等操作,就像真实用户所做的一样,从终端用户的角度测试应用程序。
与python集成
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").send_keys("selenium")
driver.find_element_by_id("su").click()
driver.quit()
selenium可以操纵各大主流浏览器chrome、firefox、ie等等,但需要下载相应的驱动包
chrome: http://chromedriver.storage.googleapis.com/index.html
firefox:https://github.com/mozilla/geckodriver/releases/
ie:http://selenium-release.storage.googleapis.com/index.html
webdriver(即:浏览器对象)基本使用方法
打开关闭标签页
#打开
def get(self, url) #关闭
def close(self) #退出浏览器
def quit(self)
设置浏览器宽高
def set_window_size(self, width, height, windowHandle='current'):
"""
Sets the width and height of the current window. (window.resizeTo) :Args:
- width: the width in pixels to set the window to
- height: the height in pixels to set the window to :Usage:
driver.set_window_size(800,600)
"""
对象定位
#通过id方式定位
driver.find_element_by_id("kw")#通过name方式定位
driver.find_element_by_name("wd") #通过tag name方式定位
driver.find_element_by_tag_name("input") #通过class name 方式定位
driver.find_element_by_class_name("s_ipt") #通过CSS方式定位
driver.find_element_by_css_selector("#kw") #通过xphan方式定位
driver.find_element_by_xpath("//input[@id='kw']") #通过link方式定位
driver.find_element_by_link_text("贴 吧") #Partial Link Text 定位
driver.find_element_by_partial_link_text("贴") #通过by指定方法类型定位
driver.find_element(By.ID, 'foo')
定位一组元素
#与上面类似加上s,但上面会抛出NoSuchElementException,下面找不到则返回empty list
#通过by指定方法类型定位
driver.find_elements(By.ID, 'foo')
框架和窗口定位
def switch_to(self):
"""
:Returns:
- SwitchTo: an object containing all options to switch focus into :Usage:
element = driver.switch_to.active_element
alert = driver.switch_to.alert
driver.switch_to.default_content()
driver.switch_to.frame('frame_name')
driver.switch_to.frame(1)
driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
driver.switch_to.parent_frame()
driver.switch_to.window('main')
"""
执行js
def execute_script(self, script, *args):
"""
Synchronously Executes JavaScript in the current window/frame. :Args:
- script: The JavaScript to execute.
- \*args: Any applicable arguments for your JavaScript. :Usage:
driver.execute_script('return document.title;')
""" def execute_async_script(self, script, *args):
"""
Asynchronously Executes JavaScript in the current window/frame. :Args:
- script: The JavaScript to execute.
- \*args: Any applicable arguments for your JavaScript. :Usage:
script = "var callback = arguments[arguments.length - 1]; " \
"window.setTimeout(function(){ callback('timeout') }, 3000);"
driver.execute_async_script(script)
"""
webelement(元素)基本使用方法
点击
driver.find_element_by_id("su").click()
driver.find_element_by_id("su").submit()
输入文本
driver.find_element_by_id("kw").send_keys("xxx")
获取属性/文本
driver.find_element_by_id("kw").text()
driver.find_element_by_id("kw").get_attribute()
driver.find_element_by_id("kw").get_property()
层次定位
#与webdiriver操作一样,可以以当前元素为父元素查找子元素
parent = driver.find_element(By.ID, 'parent')
parent.find_element(By.ID, 'child')
python爬虫入门(4)----- selenium的更多相关文章
- Python爬虫入门一之综述
大家好哈,最近博主在学习Python,学习期间也遇到一些问题,获得了一些经验,在此将自己的学习系统地整理下来,如果大家有兴趣学习爬虫的话,可以将这些文章作为参考,也欢迎大家一共分享学习经验. Pyth ...
- python爬虫入门-开发环境与小例子
python爬虫入门 开发环境 ubuntu 16.04 sublime pycharm requests库 requests库安装: sudo pip install requests 第一个例子 ...
- python爬虫动态html selenium.webdriver
python爬虫:利用selenium.webdriver获取渲染之后的页面代码! 1 首先要下载浏览器驱动: 常用的是chromedriver 和phantomjs chromedirver下载地址 ...
- Python爬虫入门教程 48-100 使用mitmdump抓取手机惠农APP-手机APP爬虫部分
1. 爬取前的分析 mitmdump是mitmproxy的命令行接口,比Fiddler.Charles等工具方便的地方是它可以对接Python脚本. 有了它我们可以不用手动截获和分析HTTP请求和响应 ...
- Python爬虫入门教程 43-100 百思不得姐APP数据-手机APP爬虫部分
1. Python爬虫入门教程 爬取背景 2019年1月10日深夜,打开了百思不得姐APP,想了一下是否可以爬呢?不自觉的安装到了夜神模拟器里面.这个APP还是比较有名和有意思的. 下面是百思不得姐的 ...
- Python 爬虫入门(二)——爬取妹子图
Python 爬虫入门 听说你写代码没动力?本文就给你动力,爬取妹子图.如果这也没动力那就没救了. GitHub 地址: https://github.com/injetlee/Python/blob ...
- Python爬虫入门之正则表达式
在前面我们已经搞定了怎样获取页面的内容,不过还差一步,这么多杂乱的代码夹杂文字我们怎样把它提取出来整理呢?下面就开始介绍一个十分强大的工具,正则表达式! 1.了解正则表达式 正则表达式是对字符串操作的 ...
- Python爬虫入门之Cookie的使用
本节我们一起来看一下Cookie的使用. 为什么要使用Cookie呢? Cookie,指某些网站为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据(通常经过加密) 比如说有些网站需要 ...
- Python爬虫入门之Urllib库的高级用法
1.设置Headers 有些网站不会同意程序直接用上面的方式进行访问,如果识别有问题,那么站点根本不会响应,所以为了完全模拟浏览器的工作,我们需要设置一些Headers 的属性. 首先,打开我们的浏览 ...
- Python爬虫入门之Urllib库的基本使用
那么接下来,小伙伴们就一起和我真正迈向我们的爬虫之路吧. 1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器解 ...
随机推荐
- java关于传值与传引用
关于java传值还是传引用的问题经常出现在一些烦人的面试题中,主要考察个人对java基础的掌握情况. 首先明确一下:本地方法中,java的参数传递都是传值.但是如果是远程调用方法时,会将对象本身传递过 ...
- Spring 面试详解
SpringSpring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象.Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地 ...
- 使用spring-test时报错
java.lang.NoClassDefFoundError: org/springframework/core/annotation/MergedAnnotations$SearchStrategy ...
- webstom 汉化,激活
1.激活 本地服务器激活: 下载 magnet:?xt=urn:btih:2289E4F8CEB346AC44E54C8C0DA706CC537301AA 得到一个压缩包IntelliJIDEALic ...
- python中 _、__、__xx__() 区别及使用场景
1.访问权限(private.public)与继承方式(只有public继承) 在面向对象编程语言中,类的属性与方法都会设置访问控制权限,从而满足我们的设计需求.一般而言,我们通常会将对象的属性设置为 ...
- 泛型<T>,是你肿莫了,还是我错了...
委托自定义数组排序 项目一共三个文件如下. CSort.cs using System; using System.Collections.Generic; using System.Linq; us ...
- java语言进阶(二)_Collection_泛型
主要内容 Collection集合 迭代器 增强for 泛型 第一章 Collection集合 1.1 集合概述 在前面基础班我们已经学习过并使用过集合ArrayList ,那么集合到底是什么呢? 集 ...
- Codeforces 1215D Ticket Game 题解
Codeforces 1215D Ticket Game 原题 题目 Monocarp and Bicarp live in Berland, where every bus ticket consi ...
- day04总结
print("陈少最帅!!!") 输出结果: 陈少最帅!!! 可以变,不可变数据类型#1.可变类型:list,dict#在值改变的情况下,id号不变,也就是说内存地址不变,证明就是 ...
- Pop!_OS安装与配置(四):GNOME插件篇
Pop!_OS安装与配置(四):GNOME插件篇 #0x0 效果图 #0x1 自动安装(不保证成功性) #0x2 OpenWeather #0x3 Topicons Plus #0x4 System- ...