0


python数据分析之爬虫基础:selenium详细讲解

目录


1、selenium介绍

(1)selenium是一个用于web应用程序测试的工具。

(2)selenium测试直接运行在浏览器中,就像真正的用户在操作一样。

(3)支持通过各种driver(FirfoxDriver,IternetExplorerDriver,OperaDriver,ChromeDriver)驱动真是浏览器完成测试。

(4)selenium也是支持无界面浏览器操作的。

2、selenium的作用:

(1)可以模拟用户在浏览器中的各种操作,如点击按钮、输入文本、提交表单等,用于对web应用程序进行功能测试,回归测试等。

(2)可以用于自动化一些重复性的网页操作任务,如批量上传文件、定时执行任务,提高工作效率。

(3)爬取一些我们无法获取的数据,比如京东上的限时秒杀数据等等。

3、配置浏览器驱动环境及selenium安装

(1)操作chrome浏览器下载

浏览器的驱动要下载和浏览器配套的版本。将下载的浏览器驱动放到python的安装目录下,并配置系统环境变量。

4fdb8ff80e8c4efeaba853edd4050a5e.png安装地址:官网

(2)selenium的安装

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple selenium

(3)测试浏览器驱动是否正常

  1. from selenium import webdriver
  2. # 创建浏览器操作对象
  3. driver = webdriver.Chrome()
  4. driver.get('http://www.baidu.com')
  5. input()

4、selenium基本语法

4.1、selenium元素的定位

元素定位:自动化要做的就是模拟鼠标和键盘来操作这些元素,点击、输入等等。操作这些元素首先要找到他们,webdriver提供很多元素定位的方法。

(1)id可以唯一定位到一个元素(以百度的百度一下四个字为例)

  1. button =driver.find_element(By.ID,"su")

(2)name要确保是全局唯一的(以百度的文本搜索框为例)

  1. button = driver.find_element(By.NAME,"wd")

(3)xpath全局唯一

  1. button = driver.find_element(By.XPATH,"//input[@id='su']")

(4)tag name标签,即标签名字

  1. button = driver.find_element(By.TAG_NAME,"input")

(5)css selector元素样式(通过bs4的语法来获取对象)

  1. button = driver.find_element(By.CSS_SELECTOR, '#su')

(6)link text获取链接文本

  1. button = driver.find_element(By.LINK_TEXT, '新闻')

4.2、selenium元素的信息

(1)通过get_attribute来获取class的属性值

  1. input = driver.find_element(By.ID, 'su')
  2. print(input.get_attribute('class'))

(2)通过text获取元素文本(只能获取两个标签之间的数据哦)

  1. a = driver.find_element(By.LINK_TEXT, '新闻')
  2. print(a.text)

(3)通过tag_name获取标签名

  1. input = driver.find_element(By.ID, 'su')
  2. print(input.tag_name)

4.3、selenium元素的交互

(1)click点击对象(以点击百度一下按钮为例)

  1. button = driver.find_element(By.ID,"su").click()

(2)send_keys在对象上模拟按键输入(搜索框中输入周杰伦)

  1. input = driver.find_element(By.ID,"kw").send_keys("周杰伦")

(3)滑到底部

  1. js_bottom = "document.documentElement.scrollTop=10000"
  2. driver.execute_script(js_bottom)

(4)回退选项

  1. driver.back()

(5)返回上一选项

  1. driver.forward()

案例:在百度搜索框中搜索周杰伦,翻到最后一页,打开下一页,回退选项,在返回上一选项。最后退出浏览器

  1. from selenium import webdriver
  2. import time
  3. from selenium.webdriver.common.by import By
  4. driver = webdriver.Chrome()
  5. url = "https://www.baidu.com"
  6. driver.get(url)
  7. time.sleep(2)
  8. input = driver.find_element(By.ID,"kw").send_keys("周杰伦")
  9. time.sleep(2)
  10. button = driver.find_element(By.ID,"su").click()
  11. time.sleep(2)
  12. js_bottom = "document.documentElement.scrollTop=10000"
  13. driver.execute_script(js_bottom)
  14. time.sleep(2)
  15. # 获取下一页的按钮
  16. next_button = driver.find_element(By.XPATH,"//a[@class='n']").click()
  17. time.sleep(2)
  18. # 回退,回到上一页
  19. driver.back()
  20. time.sleep(2)
  21. # 返回上一选项
  22. driver.forward()
  23. time.sleep(5)
  24. driver.quit()

5、Phantomjs介绍

(1)是一个无界面的浏览器

(2)支持页面元素查找,js的执行等

(3)由于不进行css和gui渲染,运行效率要比真实的浏览器要快很多

但是Phantomjs这个公司已经黄了,维护者已经辞职并停止维护了,因此这里就不讲解了。

6、chrome handless模式

chrome-handless模式,Google针对chrome浏览器59版本新增加的一种模式,可以在不打开UI界面的情况下使用chrome浏览器,所以运行效果与chrome保持一致。

系统要求:

  1. chrome
  2. Unix/Linux 系统需要 chrome >=59
  3. windows 系统需要 chrome >=60
  4. python版本 >=3.6
  5. selenium版本 >=3.4.*
  6. chromedriver版本 >=2.31
  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.chrome.options import Options
  4. chrome_options = Options()
  5. chrome_options.add_argument('--headless')
  6. chrome_options.add_argument('--disable-gpu')
  7. driver = webdriver.Chrome(options=chrome_options)
  8. url = "https://www.baidu.com"
  9. driver.get(url)
  10. driver.save_screenshot("screenshot.png")

但这样每次都需要配置的话会比较麻烦,我们只要封装到函数里面,那么每次用只需要调用函数

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.options import Options
  3. def share_browser():
  4. chrome_options = Options()
  5. chrome_options.add_argument('--headless')
  6. chrome_options.add_argument('--disable-gpu')
  7. driver = webdriver.Chrome(options=chrome_options)
  8. return driver
  9. driver = share_browser()
标签: python 爬虫 selenium

本文转载自: https://blog.csdn.net/2401_83283514/article/details/144300262
版权归原作者 人间无解 所有, 如有侵权,请联系我们删除。

“python数据分析之爬虫基础:selenium详细讲解”的评论:

还没有评论