python爬虫selenium模块详解
作者:南岸青栀* 时间:2023-04-11 20:32:43
目录
selenium模块
selenium基本概念
基本使用
基于浏览器自动化的操作
selenium处理iframe:
selenium模拟登陆QQ空间
无头浏览器和规避检测
selenium模块
selenium基本概念
selenium优势
便捷的获取网站中动态加载的数据
便捷实现模拟登陆
selenium使用流程:
1.环境安装:pip install selenium
2.下载一个浏览器的驱动程序(谷歌浏览器)
3.实例化一个浏览器对象
基本使用
代码
from selenium import webdriver
from lxml import etree
from time import sleep
if __name__ == '__main__':
bro = webdriver.Chrome(r"E:\google\Chrome\Application\chromedriver.exe")
bro.get(url='http://scxk.nmpa.gov.cn:81/xk/')
page_text = bro.page_source
tree = etree.HTML(page_text)
li_list = tree.xpath('//*[@id="gzlist"]/li')
for li in li_list:
name = li.xpath('./dl/@title')[0]
print(name)
sleep(5)
bro.quit()
基于浏览器自动化的操作
代码
#编写基于浏览器自动化的操作代码
- 发起请求: get(url)
- 标签定位: find系列的方法
- 标签交互: send_ keys( 'xxx' )
- 执行js程序: excute_script('jsCod')
- 前进,后退: back(),forward( )
- 关闭浏览器: quit()
代码
https://www.taobao.com/
from selenium import webdriver
from time import sleep
bro = webdriver.Chrome(executable_path=r"E:\google\Chrome\Application\chromedriver.exe")
bro.get(url='https://www.taobao.com/')
#标签定位
search_input = bro.find_element_by_id('q')
sleep(2)
#执行一组js代码,使得滚轮向下滑动
bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')
sleep(2)
#标签交互
search_input.send_keys('女装')
button = bro.find_element_by_class_name('btn-search')
button.click()
bro.get('https://www.baidu.com')
sleep(2)
bro.back()
sleep(2)
bro.forward()
sleep(5)
bro.quit()
selenium处理iframe:
- 如果定位的标签存在于iframe标签之中,则必须使用switch_to.frame(id)
- 动作链(拖动) : from selenium. webdriver import ActionChains
- 实例化一个动作链对象: action = ActionChains (bro)
- click_and_hold(div) :长按且点击操作
- move_by_offset(x,y)
- perform( )让动作链立即执行
- action.release( )释放动作链对象
代码
https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable
from selenium import webdriver
from time import sleep
from selenium.webdriver import ActionChains
bro = webdriver.Chrome(executable_path=r"E:\google\Chrome\Application\chromedriver.exe")
bro.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
bro.switch_to.frame('iframeResult')
div = bro.find_element_by_id('draggable')
#动作链
action = ActionChains(bro)
action.click_and_hold(div)
for i in range(5):
action.move_by_offset(17,0).perform()
sleep(0.3)
#释放动作链
action.release()
bro.quit()
selenium模拟登陆QQ空间
代码
https://qzone.qq.com/
from selenium import webdriver
from time import sleep
bro = webdriver.Chrome(executable_path=r"E:\google\Chrome\Application\chromedriver.exe")
bro.get('https://qzone.qq.com/')
bro.switch_to.frame("login_frame")
switcher = bro.find_element_by_id('switcher_plogin')
switcher.click()
user_tag = bro.find_element_by_id('u')
password_tag = bro.find_element_by_id('p')
user_tag.send_keys('1234455')
password_tag.send_keys('qwer123')
sleep(1)
but = bro.find_element_by_id('login_button')
but.click()
无头浏览器和规避检测
代码
from selenium import webdriver
from time import sleep
#实现无可视化界面
from selenium.webdriver.chrome.options import Options
#实现规避检测
from selenium.webdriver import ChromeOptions
#实现无可视化界面
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
#实现规避检测
option = ChromeOptions()
option.add_experimental_option('excludeSwitches',['enable-automation'])
bro = webdriver.Chrome(executable_path=r"E:\google\Chrome\Application\chromedriver.exe",chrome_options=chrome_options,options=option)
bro.get('https://www.baidu.com')
print(bro.page_source)
sleep(2)
bro.quit()
来源:https://blog.csdn.net/qq_43710889/article/details/115269951
标签:python,爬虫,selenium,模块
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
给你一面国旗 教你用python画中国国旗
2023-06-25 15:10:36
![](https://img.aspxhome.com/file/2023/6/109996_0s.jpg)
一次现场mysql重复记录数据的排查处理实战记录
2024-01-17 11:42:28
![](https://img.aspxhome.com/file/2023/1/94731_0s.jpg)
不管你的Python报什么错,用这个模块就能正常运行
2023-08-29 14:42:43
![](https://img.aspxhome.com/file/2023/3/62863_0s.jpg)
Python数据分析之 Pandas Dataframe应用自定义
2023-03-02 09:18:12
![](https://img.aspxhome.com/file/2023/1/97241_0s.png)
golang映射Map的方法步骤
2023-08-28 09:10:22
Python中常见的数制转换有哪些
2021-05-27 11:12:21
Python logging日志库空间不足问题解决
2021-08-24 15:59:17
![](https://img.aspxhome.com/file/2023/8/105348_0s.png)
Python中使用MELIAE分析程序内存占用实例
2021-10-26 22:34:24
Centos7使用yum安装Mysql5.7.19的详细步骤
2024-01-24 13:01:56
![](https://img.aspxhome.com/file/2023/1/105671_0s.jpg)
手把手带你了解Python数据分析--matplotlib
2022-05-21 22:07:18
![](https://img.aspxhome.com/file/2023/4/87654_0s.png)
Oracle相关组件版本信息的介绍
2023-07-14 09:19:53
![](https://img.aspxhome.com/file/2023/4/64454_0s.png)
用ASP判断客户端浏览器语言自动跳转
2010-07-09 13:34:00
给年青设计师们的十条经验教训
2011-03-31 17:09:00
利用Python进行异常值分析实例代码
2022-06-30 16:14:59
![](https://img.aspxhome.com/file/2023/5/81835_0s.jpg)
一篇文章带你详细了解python中一些好用的库
2022-03-18 09:02:19
![](https://img.aspxhome.com/file/2023/4/99044_0s.jpg)
python+splinter自动刷新抢票功能
2023-07-08 11:33:42
asp防止同时登陆的问题
2007-10-26 12:19:00
MYSQL拒绝访问报错not allowed to connect
2024-01-16 02:20:21
SQL中from_unixtime函数的使用方法实例
2024-01-14 12:45:01
Python中的三目(元)运算符详解
2023-12-06 01:20:49