python爬虫selenium和phantomJs使用方法解析
作者:一觉昏睡人 时间:2023-02-06 19:23:38
1.selenum:三方库。可以实现让浏览器完成自动化的操作。
2.环境搭建
2.1 安装:
pip install selenium
2.2 获取浏览器的驱动程序
下载地址:
http://chromedriver.storage.googleapis.com/index.html
http://npm.taobao.org/mirrors/chromedriver/
浏览器版本和驱动版本的对应关系表:
chromedriver版本 | 支持的Chrome版本 |
---|---|
v2.46 | v71-73 |
v2.45 | v70-72 |
v2.44 | v69-71 |
v2.43 | v69-71 |
v2.42 | v68-70 |
v2.41 | v67-69 |
v2.40 | v66-68 |
v2.39 | v66-68 |
v2.38 | v65-67 |
v2.37 | v64-66 |
v2.36 | v63-65 |
v2.35 | v62-64 |
v2.34 | v61-63 |
v2.33 | v60-62 |
v2.32 | v59-61 |
v2.31 | v58-60 |
v2.30 | v58-60 |
v2.29 | v56-58 |
v2.28 | v55-57 |
v2.27 | v54-56 |
v2.26 | v53-55 |
v2.25 | v53-55 |
v2.24 | v52-54 |
v2.23 | v51-53 |
v2.22 | v49-52 |
v2.21 | v46-50 |
v2.20 | v43-48 |
v2.19 | v43-47 |
v2.18 | v43-46 |
v2.17 | v42-43 |
v2.13 | v42-45 |
v2.15 | v40-43 |
v2.14 | v39-42 |
v2.13 | v38-41 |
v2.12 | v36-40 |
v2.11 | v36-40 |
v2.10 | v33-36 |
v2.9 | v31-34 |
v2.8 | v30-33 |
v2.7 | v30-33 |
v2.6 | v29-32 |
v2.5 | v29-32 |
v2.4 | v29-32 |
所有chromedriver均可在下面链接中下载到:
http://chromedriver.storage.googleapis.com/index.html
现在有一点好的是出了按照chrome版本对应的driver,直接按照浏览器版本去找对应的driver(只对应大版本就行),不用再费心去对应了,大家可以尝试一下
有些同学说下不了,到taobao下也是可以的:
http://npm.taobao.org/mirrors/chromedriver/
定位元素的8种方式:
#使用下面的方法,查找指定的元素进行操作即可
find_element_by_id 根据id找节点
find_elements_by_name 根据name找
find_elements_by_xpath 根据xpath查找
find_elements_by_tag_name 根据标签名找
find_elements_by_class_name 根据class名字查找
# 通过id定位:
dr.find_element_by_id("kw")
# 通过name定位:
dr.find_element_by_name("wd")
# 通过class name定位:
dr.find_element_by_class_name("s_ipt")
# 通过tag name定位:
dr.find_element_by_tag_name("input")
# 通过xpath定位,xpath定位有N种写法,这里列几个常用写法:
dr.find_element_by_xpath("//*[@id='kw']")
dr.find_element_by_xpath("//*[@name='wd']")
dr.find_element_by_xpath("//input[@class='s_ipt']")
dr.find_element_by_xpath("/html/body/form/span/input")
dr.find_element_by_xpath("//span[@class='soutu-btn']/input")
dr.find_element_by_xpath("//form[@id='form']/span/input")
dr.find_element_by_xpath("//input[@id='kw' and @name='wd']")
# 通过css定位,css定位有N种写法,这里列几个常用写法:
dr.find_element_by_css_selector("#kw")
dr.find_element_by_css_selector("[name=wd]")
dr.find_element_by_css_selector(".s_ipt")
dr.find_element_by_css_selector("html > body > form > span > input")
dr.find_element_by_css_selector("span.soutu-btn> input#kw")
dr.find_element_by_css_selector("form#form > span > input")
接下来,我们的页面上有一组文本链接。
<a class="mnav" href="http://news.baidu.com" rel="external nofollow" name="tj_trnews">新闻</a>
<a class="mnav" href="http://www.hao123.com" rel="external nofollow" name="tj_trhao123">hao123</a>
# 通过link text定位:
dr.find_element_by_link_text("新闻")
dr.find_element_by_link_text("hao123")
# 通过partial link text定位:
dr.find_element_by_partial_link_text("新")
dr.find_element_by_partial_link_text("hao")
dr.find_element_by_partial_link_text("123")
Selenium库下webdriver模块常用方法的使用
控制浏览器操作的一些方法
方法 说明
set_window_size() 设置浏览器的大小
back() 控制浏览器后退
forward() 控制浏览器前进
refresh() 刷新当前页面
clear() 清除文本
send_keys (value) 模拟按键输入
click() 单击元素
submit() 用于提交表单
get_attribute(name) 获取元素属性值
is_displayed() 设置该元素是否用户可见
size 返回元素的尺寸
text 获取元素的文本
---------------------
例子:
1.chrom浏览器自动搜索
from selenium import webdriver
from time import sleep
# 创建浏览器对象 驱动为浏览器的路径
bro = webdriver.Chrome("./chromedriver.exe")
url = "https://www.baidu.com"7 # 发送请求
bro.get(url)
# 让百度指定词条的搜索
text = bro.find_element_by_id('kw')
# 发送关键字
text.send_keys('python')
# 点击搜索按钮
button = bro.find_element_by_id('su')
button.click()
# 关闭浏览器
bro.quit()
2.phantomjs * 面浏览器,其自动化流程上述操作谷歌自动化流程一致。
from selenium import webdriver
from time import sleep
bro = webdriver.PhantomJS('E:/BaiduNetdiskDownload/爬虫课件/5. 动态数据加载
爬取\phantomjs‐2.1.1‐windows/bin/phantomjs.exe')
url = "https://www.baidu.com"
# 发送请求
bro.get(url)
bro.save_screenshot('./1.png')
# 让百度指定词条的搜索
text = bro.find_element_by_id('kw')
# 发送关键字
text.send_keys('python')
bro.save_screenshot('./2.png')
# 点击搜索按钮
button = bro.find_element_by_id('su')
button.click()
sleep(3)
bro.save_screenshot('./3.png')
# 关闭浏览器
bro.quit()
3.豆瓣网电影排行榜滚动条数据
from selenium import webdriver
from time import sleep3 url = 'https://movie.douban.com/typerank?type_name=%E5%96%9C%E5%89%A7&typ
e=24&interval_id=100:90&action='
bro = webdriver.PhantomJS('E:/BaiduNetdiskDownload/爬虫课件/5. 动态数据加载
爬取\phantomjs‐2.1.1‐windows/bin/phantomjs.exe')
bro.get(url)
sleep(1)
bro.save_screenshot("./1.png")
js = 'window.scrollTo(0,document.body.scrollHeight)'
# 执行js代码 滚动条
bro.execute_script(js)
sleep(1)
bro.save_screenshot('./2.png')
# 获取页面数据
page_source = bro.page_source
print(page_source)
来源:https://www.cnblogs.com/person1-0-1/p/11320755.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mysql的列修改成行并显示数据的简单实现
![](https://img.aspxhome.com/file/2023/0/88500_0s.png)
Redis有序集合类型的操作_动力节点Java学院整理
![](https://img.aspxhome.com/file/2023/4/115964_0s.jpg)
关于搜索建议的两点小问题
js 复制功能 支持 for IE/FireFox/mozilla/ns
DSDS应用场景(高通5G)
![](https://img.aspxhome.com/file/2023/4/131934_0s.png)
如何将ChatGPT整合到Word中
![](https://img.aspxhome.com/file/2023/3/109523_0s.png)
display:inline-block的深入理解
Pytorch实现将label变成one hot编码的两种方式
MySQL 性能优化的最佳20多条经验分享
![](https://img.aspxhome.com/file/2023/8/72498_0s.jpg)
基于opencv对高空拍摄视频消抖处理方法
![](https://img.aspxhome.com/file/2023/9/133789_0s.gif)
Python集成学习之Blending算法详解
![](https://img.aspxhome.com/file/2023/1/130661_0s.jpg)
详解Python 关联规则分析
![](https://img.aspxhome.com/file/2023/9/135219_0s.png)
python 通过视频url获取视频的宽高方式
详解如何在微信小程序开发中正确的使用vant ui组件
![](https://img.aspxhome.com/file/2023/5/123715_0s.png)
mssql使用存储过程破解sa密码
Golang import 导入包语法及一些特殊用法详解
python 使用第三方库requests-toolbelt 上传文件流的示例
![](https://img.aspxhome.com/file/2023/2/94002_0s.png)
django实现前后台交互实例
![](https://img.aspxhome.com/file/2023/6/124666_0s.png)
一文详解go mod依赖管理详情
![](https://img.aspxhome.com/file/2023/2/99062_0s.png)
python实现串口通信的示例代码
![](https://img.aspxhome.com/file/2023/7/61737_0s.jpg)