爬虫 - 请求库之selenium

介绍

官方文档

selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题

selenium本质是通过驱动浏览器,完全模拟浏览器的操作,比如跳转、输入、点击、下拉等,来拿到网页渲染之后的结果,可支持多种浏览器

from selenium import webdriver
browser=webdriver.Chrome()  # 谷歌浏览器
browser=webdriver.Firefox()  # 火狐浏览器
browser=webdriver.PhantomJS()
browser=webdriver.Safari()
browser=webdriver.Edge()

安装

>: pip3 install selenium
  • 有界面浏览器

下载chromdriver.exe放到python安装路径的scripts目录中即可,注意最新版本是2.38,并非2.9
国内镜像网站地址:http://npm.taobao.org/mirrors/chromedriver/2.38/
最新的版本去官网找:https://sites.google.com/a/chromium.org/chromedriver/downloads

#验证安装
C:\Users\Administrator>python3
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> driver=webdriver.Chrome() #弹出浏览器
>>> driver.get('https://www.baidu.com')
>>> driver.page_source

#注意:
selenium3默认支持的webdriver是Firfox,而Firefox需要安装geckodriver
下载链接:https://github.com/mozilla/geckodriver/releases
selenium+chromedriver

推荐将chromdriver.exe放到项目录内

  • 无界面浏览器

下载phantomjs,解压后把phantomjs.exe所在的bin目录放到环境变量
下载链接:http://phantomjs.org/download.html

#验证安装
C:\Users\Administrator>phantomjs
phantomjs> console.log('egon gaga')
egon gaga
undefined
phantomjs> ^C
C:\Users\Administrator>python3
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> driver=webdriver.PhantomJS() #无界面浏览器
>>> driver.get('https://www.baidu.com')
>>> driver.page_source
selenium+phantomjs

注意:phantomjs已经不再更新

 selenium+谷歌浏览器headless模式

from selenium import webdriver

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('window-size=1920x3000') #指定浏览器分辨率
chrome_options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度
chrome_options.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
chrome_options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" #手动指定使
chrome_options.add_argument('disable-infobars')  # 去掉 'chrome正受到自动测试软件的控制' 提示
# chrome_options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" #手动指定使
# bro=webdriver.PhantomJS()

bro=webdriver.Chrome(chrome_options=chrome_options)
# bro = webdriver.Chrome()
bro.get('https://www.baidu.com')  # 打开目标url
print(bro.page_source)  # 获取目标页面的代码
bro.close()  # 关闭浏览器,回收资源

 使用

基本使用

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('disable-infobars')  # 去掉 'chrome正受到自动测试软件的控制' 提示

bro=webdriver.Chrome(chrome_options=chrome_options)
bro.get('https://www.baidu.com')  # 打开目标url
inp = bro.find_element_by_id('kw')  # 获取页面中的input框
inp.send_keys('python')  # 向input框内输入内容
inp.send_keys(Keys.ENTER)  # 模拟键盘回车操作
print(bro.page_source)
time.sleep(5)
bro.close()  # 关闭浏览器

猜你喜欢

转载自www.cnblogs.com/waller/p/11937631.html