python下的selenium和PhantomJS
一般我们使用python的第三方库requests及框架scrapy来爬取网上的资源,但是设计javascript渲染的页面却不能抓取,此时,我们使用web自动化测试化工具Selenium+无界面浏览器PhantomJS来抓取javascript渲染的页面, 但是新版本的Selenium不再支持PhantomJS了,请使用Chrome或Firefox的无头版本来替代。
如下图:

这里有2中解决方案, 我采用第一种, 第二种搞了很久也没有成功
一:降级selenium使用
pip uninstall selenium #先卸载selenium
pip install selenium==3.4.3 #指定版本安装selenium
防止以后官网没得下载,先备份个selenium3.4.3和PhantomJS 下载地址
二:使用Headless Chrome和Headless Firefox
使用Headless Chrome
Headless模式是Chrome 59中的新特征。
要使用Chrome需要安装 chromedriver。
chromedriver驱动大全
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')
br = webdriver.Chrome(chrome_options=chrome_options)
br.get('https://www.baidu.com/')
baidu = br.find_element_by_id('su').get_attribute('value')
print(baidu)
使用Headless Firefox
要使用Firebox需要安装 geckodriver。
geckodriver.exe驱动大全
from selenium import webdriver
from selenium.webdriver.firefox.options import Options firefox_options = Options()
firefox_options.add_argument('--headless')
br = webdriver.Firefox(firefox_options=firefox_options)
br.get('https://www.baidu.com/')
baidu = br.find_element_by_id('su').get_attribute('value')
print(baidu)
以上代码我在测试的时候没有成功, 遇到如下错误:

说的是chrome的版本不一致,当然前人也遇到过了, 只是我按照网上说的 没有解决
Python爬虫Selenium安装
使用python selenium時關於chromedriver的小問題
Selenium support for PhantomJS has been deprecated, please use headless
最后我找了一个老版本解决了问题 2.33
Python的模块pywin32中的win32gui.SystemParametersInfo()函数,在使用win32con.SPI_SETDESKWALLPAPER设置Wallpaper时,其第二个参数为图片路径,图片必须是BMP格式。如下:
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, imagepath, 1+2)
否则将报错如下:pywintypes.error: (0, 'SystemParametersInfo', 'No error message is available') 关于 SystemParametersInfo
在设置壁纸的时候发现img_path = "D://Users//Gavin//PythonDemo//Bing.bmp"失败,但是把路径改为img_path = "D:\\Users\\Gavin\\PythonDemo\\Bing.bmp" 就可以了或者img_path = "D:/Users/Gavin/PythonDemo/Bing.bmp"
Python更换Windows壁纸,问题与解决方案
使用pythonwin设置windows的桌面背景
我的demo是参考 Python爬虫之提取Bing搜索的背景图片并设置为Windows的电脑桌面
具体代码:
# -*- coding: utf- -*-
"""
此程序用于提取Bing搜索的背景图片并设置为Windows的电脑桌面
"""
from urllib.request import urlretrieve
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
import win32api,win32con,win32gui
from selenium.webdriver.chrome.options import Options # 利用PhantomJS加载网页
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
browser = webdriver.Chrome(chrome_options=chrome_options)
# 设置最大等待时间为30s
browser.set_page_load_timeout() url = 'https://cn.bing.com/'
try:
browser.get(url)
except TimeoutException:
# 当加载时间超过30秒后,自动停止加载该页面
browser.execute_script('window.stop()') # 从id为bgDiv的标签中获取背景图片的信息
t = browser.find_element_by_id('bgDiv')
bg = t.get_attribute('style') # 从字符串中提取背景图片的下载网址
start_index = bg.index('(')
end_index = bg.index(')')
img_url = bg[start_index+: end_index]
img_url = img_url.replace('"', '')
# 下载该图片到本地
img_path = "D:\\Users\\Gavin\\PythonDemo\\Bing.bmp"
urlretrieve(img_url, img_path) # 将下载后的图片设置为Windows系统的桌面
# 打开指定注册表路径
reg_key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", , win32con.KEY_SET_VALUE)
# 最后的参数:2拉伸,0居中,6适应,10填充,0平铺
win32api.RegSetValueEx(reg_key, "WallpaperStyle", , win32con.REG_SZ, "")
# 最后的参数:1表示平铺,拉伸居中等都是0
win32api.RegSetValueEx(reg_key, "TileWallpaper", , win32con.REG_SZ, "")
# 刷新桌面
try:
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, img_path, win32con.SPIF_SENDWININICHANGE)
except Exception as e:
print(e)
以上代码 确实是可以跑起来的(win10 python3.7), 但是晚上 回家后再win7 就跑不起来,典型的就是 图片问题,需要安装 pip install Pillow 于是 code 变成如下:
# -*- coding: utf- -*-
"""
此程序用于提取Bing搜索的背景图片并设置为Windows的电脑桌面
"""
from urllib.request import urlretrieve
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
import win32api, win32con, win32gui
from PIL import Image
from selenium.webdriver.chrome.options import Options # 利用PhantomJS加载网页
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
browser = webdriver.Chrome(options=chrome_options)
# 设置最大等待时间为30s
browser.set_page_load_timeout() url = 'https://cn.bing.com/'
try:
browser.get(url)
except TimeoutException:
# 当加载时间超过30秒后,自动停止加载该页面
browser.execute_script('window.stop()') # 从id为bgDiv的标签中获取背景图片的信息
t = browser.find_element_by_id('bgDiv')
bg = t.get_attribute('style') # 从字符串中提取背景图片的下载网址
start_index = bg.index('(')
end_index = bg.index(')')
img_url = bg[start_index + : end_index]
img_url = img_url.replace('"', '')
# 下载该图片到本地
img_path = "D:\\Python\\demoBing.jpg"
urlretrieve(img_url, img_path)
bmpImage = Image.open(img_path)
img_path = img_path.replace('.jpg', '.bmp')
bmpImage.save(img_path, "BMP") # 将下载后的图片设置为Windows系统的桌面
# 打开指定注册表路径
reg_key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", , win32con.KEY_SET_VALUE)
# 最后的参数:2拉伸,0居中,6适应,10填充,0平铺
win32api.RegSetValueEx(reg_key, "WallpaperStyle", , win32con.REG_SZ, "")
# 最后的参数:1表示平铺,拉伸居中等都是0
win32api.RegSetValueEx(reg_key, "TileWallpaper", , win32con.REG_SZ, "")
# 刷新桌面
try:
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, img_path, win32con.SPIF_SENDWININICHANGE)
except Exception as e:
print(e)
python下的selenium和PhantomJS的更多相关文章
- python下的selenium和chrome driver的安装
selenium是一款支持多种语言.多种浏览器.多个平台的开源web自动化测试软件,测试人员可用python.java等语言编写自动化脚本,使得浏览器可以完全按照你的指令运行,大大节省了测试人员用鼠标 ...
- 吾八哥学Selenium(一):Python下的selenium安装
selenium简介 Selenium也是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mozilla Firefox.Mo ...
- python爬虫之selenium、phantomJs
图片懒加载技术 什么是图片懒加载技术 图片懒加载是一种网页优化技术.图片作为一种网络资源,在被请求时也与普通静态资源一样,将占用网络资源,而一次性将整个页面的所有图片加载完,将大大增加页面的首屏加载时 ...
- python下的selenium安装
安装python 打开 Python官网,找到“Download”, 在其下拉菜单中选择自己的平台(Windows/Mac),一般的Linux平台已经自带的Python,所以不需要安装,通过打开“终端 ...
- Python爬虫系列-Selenium+Chrome/PhantomJS爬取淘宝美食
1.搜索关键字 利用Selenium驱动浏览器搜索关键字,得到查询后的商品列表 2.分析页码并翻页 得到商品页码数,模拟翻页,得到后续页面的商品列表 3.分析提取商品内容 利用PyQuery分析源码, ...
- Python爬虫开发【第1篇】【动态HTML、Selenium、PhantomJS】
JavaScript JavaScript 是网络上最常用也是支持者最多的客户端脚本语言.它可以收集用户的跟踪数据,不需要重载页面直接提交表单,在页面嵌入多媒体文件,甚至运行网页游戏. 我们可以在网页 ...
- Python爬虫 Selenium与PhantomJS
Selenium Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,最初是为网站自动化测试而开发的,类型像我们玩游戏用的按键精灵,可以按指定的命令自动化操作,不同是Sele ...
- python下的自动化测试--selenium 验证码输入问题
之前一直在研究scrapy下数据抓取,在研究ajax数据抓取时碰巧研究了一下selenium,确实很实用,不过只做scrapy下的数据抓取,不怎么合适,一是性能的损耗,一直需要开一个浏览器,二是对于爬 ...
- windows环境下安装selenium+python
selenium 是一个web的自动化测试工具,不少学习功能自动化的同学开始首选selenium ,相因为它相比QTP有诸多有点: * 免费,也不用再为破解QTP而大伤脑筋 * 小巧,对于不同的语 ...
随机推荐
- 关于mac远程链接window服务器以及实现共享文件
要最近换了新电脑mac 虽然运行速度666 但是真的很多地方都使用不习惯 这里记录一下 关于 远程链接window主机的问题 方便以后用 首先是 链接: 在应用里 找到 然后类似于 wind ...
- 51Nod1336 RMQ逆问题 其他
原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1336.html 题目传送门 - 51Nod1336 题意 题解 我们将输入的一个区间的答案称为 V ...
- P1087 FBI树 二叉树
题目描述 我们可以把由“00”和“11”组成的字符串分为三类:全“00”串称为BB串,全“11”串称为I串,既含“00”又含“11”的串则称为F串. FBIFBI树是一种二叉树,它的结点类型也包括FF ...
- 010 pandas的DataFrame
一:创建 1.通过二维数组进行创建 2.取值 取列,取位置的值 3.切片取值 这个和上面的有些不同,这里先取行,再取列 4.设定列索引 这里使用的行索引与上面不同. 5.通过字典的方式创建 6.索引 ...
- 20165235 祁瑛 2018-3 《Java程序设计》第四周学习总结
20165235 祁瑛 2018-3 <Java程序设计>第四周学习总结 教材学习内容总结 第五单云总结 (一)子类与继承1.java中子类只能有一个父类,在类名前用extends标记.2 ...
- day52 js--- bom dom
本文转载自李文周博客,-----cnblog.liwenzhou.com dom官网资料: http://www.w3school.com.cn/htmldom/dom_methods.asp Jav ...
- gevent实现生产者消费者
from gevent import monkey;monkey.patch_all()from gevent.queue import Queue #队列 gevent中的队列import geve ...
- 【python】常用第三方模块
No1: [Pillow]图像处理标准库 缩放 from PIL import Image # 打开一个jpg图像文件,注意是当前路径: im = Image.open('test.jpg') # 获 ...
- 从零搭建 ES 搜索服务(六)相关性排序优化
一.前言 上篇介绍了搜索结果高亮的实现方法,本篇主要介绍搜索结果相关性排序优化. 二.相关概念 2.1 排序 默认情况下,返回结果是按照「相关性」进行排序的--最相关的文档排在最前. 2.1.1 相关 ...
- spring boot整合servlet、filter、Listener等组件方式
创建一个maven项目,然后此项目继承一个父项目:org.springframework.boot 1.创建一个maven项目: 2.点击next后配置父项目及版本号 3.点击finish后就可查看p ...