python 爬虫学习之 selenium.webdriver学习

适用:爬取动态页面数据

谷歌浏览器驱动程序下载地址:
http://chromedriver.storage.googleapis.com/index.html

1 浏览器创建

实例化一款浏览器

browser = webdriver.Chrome(executable_path=‘chromedriver.exe’)

from selenium import webdriverbrowser = webdriver.Chrome()
browser = webdriver.Firefox()
browser = webdriver.Edge()
browser = webdriver.PhantomJS()
browser = webdriver.Safari()

2 元素定位

 注: find_element_by_xxx找的是第一个符合条件的标签,find_elements_by_xxx找的是所有符合条件的标签。

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keysclass Douban(object):def __init__(self):self.url = 'https://accounts.douban.com/passport/login?source=book'#创建浏览器self.driver = webdriver.Chrome()def LogIn(self):#通过浏览器向服务器发送URL请求self.driver.get(self.url)time.sleep(3)# 生成一张网页快照self.driver.save_screenshot('123.png')# 点击账号密码登录(通过class元素定位)self.driver.find_element_by_class_name('account-tab-account').click()# 手机账号输入(通过id元素定位)self.driver.find_element_by_id('username').send_keys('手机号码')time.sleep(2)self.driver.find_element_by_id('username').send_keys(Keys.TAB)#(通过name元素定位)self.driver.find_element_by_name('password').send_keys('密码')#(通过"文本链接"定位)self.driver.find_element_by_link_text('登录豆瓣').click()'''#(通过xpath定位)self.driver.find_element_by_xpath("//*[@]")self.driver.find_element_by_xpath("//*[@name='wd']")self.driver.find_element_by_xpath("//input[@]")self.driver.find_element_by_xpath("/html/body/form/span/input")self.driver.find_element_by_xpath("//span[@]/input")self.driver.find_element_by_xpath("//form[@]/span/input")self.driver.find_element_by_xpath("//input[@ and @name='wd']")#(通过tag元素定位)self.driver.find_element_by_tag_name("input")'''time.sleep(3)self.driver.save_screenshot('345.png')#打印cookies信息print(self.driver.get_cookies())if __name__ == '__main__':douban = Douban()douban.LogIn()

3 控制浏览器

from selenium import webdriver
from time import sleep#1.创建Chrome浏览器对象,这会在电脑上在打开一个浏览器窗口
browser = webdriver.Chrome(executable_path= "chromedriver.exe")
#2.通过浏览器向服务器发送URL请求
browser.get("https://www.baidu.com/")
sleep(3)
#3.刷新浏览器
browser.refresh()
#4.设置浏览器的大小
browser.set_window_size(1400,800)
#5.设置链接内容
element=browser.find_element_by_link_text("新闻")
element.click()

4 调用JavaScript代码

from selenium import webdriver
from time import sleep# 1.访问百度
drive = webdriver.Chrome(executable_path='chromedriver.exe')
drive.get('https://www.baidu.com')# 2.搜索
drive.find_element_by_id('kw').send_keys('python')
drive.find_element_by_id('su').click()# 3.休眠2s,获取服务器的响应内容
sleep(2)# 4.通过javascript设置浏览器窗口的滚动条位置
drive.execute_script('window.scrollTo(0, 500)')
# drive.execute_script('window.scrollTo(0, document.body.scrollHeight)') #滑到最底部sleep(2)
drive.close()

5 获取页面源码数据

from selenium import webdriver
from time import sleep# 1.访问百度
drive = webdriver.Chrome(executable_path='chromedriver.exe')
drive.get('https://www.baidu.com')# 2.搜索
drive.find_element_by_id('kw').send_keys('python')
drive.find_element_by_id('su').click()# 3.休眠2s,获取服务器的响应内容
sleep(2)# 4.获取页面源码数据
text = drive.page_source
print(text)drive.close()

6 cookie操作

from selenium import webdriver
drive = webdriver.Chrome(executable_path='chromedriver.exe')
drive.get('https://www.cnblogs.com/')# 1.打印cookie信息
print(drive.get_cookies())# 2.添加cookie信息
dic = {'name':'name', 'value':'python'}
drive.add_cookie(dic)
print(drive.get_cookies())# 3.遍历打印cookie信息
for cookie in drive.get_cookies():print(f"{cookie['name']}---f{cookie['value']}\n")drive.close()

7 谷歌无头浏览器

from selenium import webdriver
from selenium.webdriver.chrome.options import Options# 1.创建一个参数对象,用来控制chrome以无界面模式打开
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')# 2.创建浏览器对象
drive = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=chrome_options)# 3.发起请求获取数据
drive.get('https://www.cnblogs.com/')page_text = drive.page_source
print(page_text)drive.close()

8 selenium规避被检测识别

from selenium import webdriver
from selenium.webdriver import ChromeOptions# 1.实例化一个ChromeOptions对象
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])# 2.将ChromeOptions实例化的对象option作为参数传给Crhome对象
driver = webdriver.Chrome(executable_path='chromedriver.exe', options=option)# 3.发起请求
driver.get('https://www.taobao.com/')

python 爬虫学习之 selenium.webdriver学习相关推荐

  1. 用 python selenium 爬简书,Python自动化领域之 Selenium WebDriver 学习第2篇

    本篇博客使用 selenium 实现对简书官网的操作. 文章目录 通过 selenium 执行 JS selenium 实现简书搜索 selenium 隐式与显式等待 selenium 采集京东图书 ...

  2. 通过简书网学习 ActionChains,selenium webdriver 学习第3篇

    本篇博客学习 selenium webdriver 控制窗口句柄,以及模拟鼠标键盘操作等内容. 控制 BOSS 直聘网站窗口句柄 本次先通过 BOSS 直聘网进行测试,打开网站首页的头图. 切换句柄, ...

  3. selenium webdriver学习(一)------------快速开始(转载JARVI)

    selenium webdriver学习(一)------------快速开始 博客分类: Selenium-webdriver selenium webdriver 学习 selenium webd ...

  4. selenium webdriver学习(八)------------如何操作select下拉框(转)

    selenium webdriver学习(八)------------如何操作select下拉框 博客分类: Selenium-webdriver 下面我们来看一下selenium webdriver ...

  5. Python爬虫4.4 — selenium高级用法教程

    Python爬虫4.4 - selenium高级用法教程 综述 Headless Chrome 设置请求头 设置代理IP 常用启动项参数options设置 Cookie操作 selenium设置coo ...

  6. python爬虫之初恋 selenium

    selenium 是一个web应用测试工具,能够真正的模拟人去操作浏览器. 用她来爬数据比较直观,灵活,和传统的爬虫不同的是, 她真的是打开浏览器,输入表单,点击按钮,模拟登陆,获得数据,样样行.完全 ...

  7. python爬虫代理和selenium

    python爬虫代理和selenium 1.代理ip的使用 1.1 获取蘑菇代理中的代理ip def get_ip():response=requests.get('http://piping.mog ...

  8. Python爬虫 | 一条高效的学习路径

    不推课程,直接上干货!(文末附python爬虫学习资料,都是我之前用过的,免费的) 从环境配置,到基础知识了解,再到爬虫实战,手把手带你入门Python爬虫. 本文主要针对入门,如果寻求进阶,或者在爬 ...

  9. python爬虫实训日志_Python学习学习日志——爬虫《第一篇》(BeautifulSoup)

    爬虫简介(学习日志第一篇) 一.爬虫介绍 爬虫:一段自动抓取互联网信息的程序,从互联网上抓取对于我们有价值的信息. 二.Pyyhon爬虫架构 Python 爬虫架构主要由五个部分组成,分别是调度器.U ...

  10. python爬虫有趣的应用软件_Python学习,爬虫不一定非要抓数据,也可以做自己喜欢的应用程序...

    写在前面的话 最近各种负面消息,对爬虫er来说,并不是很友好,当然这个是对于从业者来说的,对像我这样的正在学习python的个人来说,python爬虫的学习只需要保持以下几点,基本不会出现大的问题:遵 ...

最新文章

  1. char和uchar区别
  2. harmonyos分层,HarmonyOS开发--1、组件化的设计方案
  3. mycat是什么_MYCAT学习2
  4. 伯努利分布方差_统计知识(4)——分布
  5. 图解MySql命令行创建存储过程
  6. 【任务脚本】0523更新京东618叠蛋糕任务脚本全自动脚本,大神更新了京东任务全自动程序...
  7. 政府项目需要注意的事项
  8. Docker与自动化测试及其测试实践
  9. iview select 怎么清空_在使用iview时发现要先重置一下表单然后填写完后再重置可以清空Select多选框,否则清不掉,什么原因?...
  10. android TextView EditTextView一些技巧使用 (视图代码布局)
  11. 机房系统(一)——【修改密码 登录 】
  12. [Python] L1-022. 奇偶分家-PAT团体程序设计天梯赛GPLT
  13. java中的equals和hashCode
  14. Openstack(十四)创建虚拟机
  15. 协议森林02 小喇叭开始广播 (以太网与WiFi协议)
  16. 关于KX混响插件:REVERB R详解
  17. Project 制作工作进度计划 排除休息日
  18. 通过蒲公英快速查询苹果手机UDID方法
  19. 那些烦人的VC++库、win10中的VC++库(全部)
  20. arduino动态刷新显示_玩家国度XG27UQ绝影游戏显示器评测:DSC加持的满血4K显示器...

热门文章

  1. Pr:子剪辑子序列嵌套编组
  2. 恒玄BES调试笔记-BES2500啸叫howling
  3. 还记得那年大明湖畔的Java 7吗
  4. Xenu Link Sleuth 简单好用的链接测试工具 使用说明
  5. 怎么更改wifi频段_我告诉你wifi频段怎么设置
  6. Python 繁体转简体
  7. C++基础中的基础——平行四边形
  8. warp-transducer,warprnnt_tensorflow
  9. 使用XMLSerializer报错java.lang.NoClassDefFoundError: nu/xom/ParentNode
  10. 光剑评注:其实,说了这么多废话,无非就是: 一切皆是映射。不管是嵌套 XML,还是 Lisp 嵌套括号,还是 XXX 的 Map 数据结构,一切都是树形结构——映射。...