Python爬虫学习笔记(八)————Phantomjs与Chrome handless

目录

1.Phantomjs

(1)什么是Phantomjs?

(2)如何使用Phantomjs?

[2 .Chrome handless](#2 .Chrome handless)

(1)系统要求:

(2)配置:

(3)配置封装:

(4)封装调用:


1.Phantomjs

(1)什么是Phantomjs?

①是一个无界面的浏览器

②支持页面元素查找,js的执行等

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

(2)如何使用Phantomjs?

①获取PhantomJS.exe文件路径path

②browser = webdriver.PhantomJS(path)

③browser.get(url)

扩展:保存屏幕快照:browser.save_screenshot('baidu.png')

python 复制代码
from selenium import webdriver

path = 'phantomjs.exe'

browser = webdriver.PhantomJS(path)


url = 'https://www.baidu.com'
browser.get(url)

browser.save_screenshot('baidu.png')

import time
time.sleep(2)

input = browser.find_element_by_id('kw')
input.send_keys('昆凌')

time.sleep(3)

browser.save_screenshot('kunling.png')

目前Phantomjs已经停止更新 ,会提醒你使用下面的handless

2 .Chrome handless

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

(1)系统要求:

Chrome

Unix\Linux 系统需要 chrome >= 59

Windows 系统需要 chrome >= 60

Python3.6

Selenium==3.4.*

ChromeDriver==2.31

(2)配置:

from selenium import webdriver

from selenium.webdriver.chrome.options import Options

chrome_options = Options()

chrome_options.add_argument('‐‐headless')

chrome_options.add_argument('‐‐disable‐gpu')

path是你自己的chrome浏览器的文件路径

path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'

chrome_options.binary_location = path

browser = webdriver.Chrome(chrome_options=chrome_options)

browser.get('http://www.baidu.com/')

(3)配置封装:

from selenium import webdriver

#这个是浏览器自带的 不需要我们再做额外的操作

from selenium.webdriver.chrome.options import Options

def share_browser():

#初始化 chrome_options = Options()

chrome_options.add_argument('‐‐headless')

chrome_options.add_argument('‐‐disable‐gpu')

#浏览器的安装路径 打开文件位置

#这个路径是你谷歌浏览器的路径

path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'

chrome_options.binary_location = path

browser = webdriver.Chrome(chrome_options=chrome_options)

return browser

(4)封装调用:

from handless import share_browser

browser = share_browser()

browser.get('http://www.baidu.com/')

browser.save_screenshot('handless1.png')

python 复制代码
# from selenium import webdriver
# from selenium.webdriver.chrome.options import Options
#
# chrome_options = Options()
# chrome_options.add_argument('--headless')
# chrome_options.add_argument('--disable-gpu')
#
# path是你自己的chrome浏览器的文件路径
# path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
# chrome_options.binary_location = path
#
# browser = webdriver.Chrome(chrome_options=chrome_options)
#
#
# url = 'https://www.baidu.com'
#
# browser.get(url)
#
# browser.save_screenshot('baidu.png')

# 封装的handless


from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def share_browser():
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')

    # path是你自己的chrome浏览器的文件路径
    path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
    chrome_options.binary_location = path

    browser = webdriver.Chrome(chrome_options=chrome_options)
    return browser

browser = share_browser()

url = 'https://www.baidu.com'

browser.get(url)

handless经过上面的固定配置或者将配置封装后,其他的操作与selenium一致,但是handless不显示界面,速度更快。

相关推荐
天启HTTP2 小时前
爬虫防封核心原理:IP轮换机制与风控规避逻辑
linux·服务器·网络·爬虫·tcp/ip
全栈练习生4 小时前
AI Agent 沙箱
python·ai
SEO_juper5 小时前
用 Python 写一个 GEO 可见性检查脚本:你的网站现在能被 AI 引用吗
开发语言·人工智能·爬虫·python·seo·外贸独立站
用户8356290780516 小时前
使用 Python 对 Excel 工作表进行排序
后端·python
for_ever_love__7 小时前
MySQL 全文索引实战:FULLTEXT、ngram 中文分词与 MATCH AGAINST 到底该怎么用
java·python·mysql·全文检索·分词·索引·ngram
用户8356290780517 小时前
使用 Python 合并 PowerPoint 演示文稿
后端·python
weixin_461769407 小时前
VS Code 中创建 Jupyter 文件(.ipynb)
ide·python·jupyter
2601_966949657 小时前
多市场量化策略的数据接口应该如何设计:从数据层架构到策略接入
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
全栈弄潮儿7 小时前
Python实战第1期:Python环境搭建与第一个程序
python
心易行者7 小时前
Agent应用+API端点商业化进阶实战:从单体智能体到可付费调用的API全流程
运维·服务器·人工智能·python·apache