惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
The Register - Security
The Register - Security
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The GitHub Blog
The GitHub Blog
博客园 - 司徒正美
罗磊的独立博客
U
Unit 42
S
SegmentFault 最新的问题
Y
Y Combinator Blog
博客园_首页
Hugging Face - Blog
Hugging Face - Blog
J
Java Code Geeks
Schneier on Security
Schneier on Security
Know Your Adversary
Know Your Adversary
C
Check Point Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
V
Vulnerabilities – Threatpost
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
The Hacker News
The Hacker News
博客园 - 叶小钗
C
Cybersecurity and Infrastructure Security Agency CISA
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Latest news
Latest news
L
Lohrmann on Cybersecurity
A
About on SuperTechFans
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
S
Securelist
A
Arctic Wolf
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Threatpost
Scott Helme
Scott Helme
博客园 - 聂微东
博客园 - 【当耐特】
T
Tenable Blog
I
Intezer
D
DataBreaches.Net
B
Blog RSS Feed
Security Latest
Security Latest
C
Cisco Blogs
T
Tor Project blog
N
Netflix TechBlog - Medium

🛫Qifei's Blog

Python的模块构建和调用方式 - 🛫Qifei's Blog 如何选择搭建梯子用的VPS? - 🛫Qifei's Blog Hexo博客页面引入足迹地图 - 🛫Qifei's Blog 机器学习和scikit-learn库基础学习笔记 - 🛫Qifei's Blog 利用国内服务器加速v2ray访问速度和规避检查 - 🛫Qifei's Blog Matplotlib_Bar3d绘制彩色带颜色标尺的3D柱形图 - 🛫Qifei's Blog Linux后台运行nostr虚荣公钥的挖掘 - 🛫Qifei's Blog Hexo博客添加Nostr_NIP-05认证 - 🛫Qifei's Blog Nostr_NIP-05认证服务简介和配置 - 🛫Qifei's Blog Onedrive_API大文件上传记录 - 🛫Qifei's Blog 反爬虫-如何检测有没有使用Puppeteer - 🛫Qifei's Blog Vuejs学习笔记 - 🛫Qifei's Blog OpenOffice连接Mysql数据库 - 🛫Qifei's Blog 安卓ToyVPN服务端从零开始读 - 🛫Qifei's Blog 在控制器中用JS验证CSS_Selector和Xpath - 🛫Qifei's Blog RWTH自习室自动预定程序(RWTH_Lernraum) - 🛫Qifei's Blog RWTH自习室自动预定程序(RWTH_Lernraum) - 🛫Qifei's Blog Python如何准确的计算Http请求中的Content_Length - 🛫Qifei's Blog 破解图片等资源跨域和防盗链阻拦 - 🛫Qifei's Blog Selenium-Webdriver接口 - 🛫Qifei's Blog
如何利用Selenium实现更加高效的爬虫 - 🛫Qifei's Blog
Qifei · 2021-07-09 · via 🛫Qifei's Blog

1. 导语

Selenium作为浏览器自动化工具,由于其可以直接执行js代码的优越性,经常被用做爬虫工具。但是,毕竟需要控制浏览器,低效率高内存始终是其难以甩掉的标签。在我个人爬虫开发中,对如何高效利用selenium有了一套个人的理解。

2. 用js注入代替selenium原生操作

selenium库提供的execute_script方法使js注入成为可能。下面是一个js注入的例子

1
2
js="document.getElementsByClassName('form-control')[0].value='%s';"%(requestCode[i][0].value)
driver.execute_script(js)

以selenium填写表单为例,python代码如下:

1
2
vorname = driver.find_element_by_name('vorname')
vorname.send_keys('vorname')

使用selenium填写表单,执行速度是相当慢的,当input标签很多的时候,selenium会从上到下依次执行表单填写,这就会导致在填写完整的表单时,填写时间其实比人为通过浏览器autocomplete快不了多少,甚至更慢。

使用js来执行表单填写,如下代码:

1
2
fill_form_script = "bsform.vorname.value='vorname';bsform.submit()"
driver.execute_script(fill_form_script)

bsform为form标签的name属性,vorname为input标签的name属性,通过bsform.vorname.value代码快速填写表单,多个input几乎是同时完成,在调用submit()方法提交表单,甚至可以直接绕过浏览器的表单验证。相比selenium模拟操作更快,代码也更加简介。

3. 利用ajax代替selenium打开多个窗口

我们在使用selenium时,最主要还是看重,它直接可以执行网页中的js,而无需在去分析js代码的特点。但是受限于,其高内存占用的缺点,很难在多线程时执行爬虫任务。利用js注入可以在网页中执行js代码,既然如此,那是否可以注入js直接用ajax来请求网页,如果可以,相比较于利用requests库,这样可以极大的避免被网页服务器的反爬虫机制检测到,也能绕开分析js来获取一些参数,甚至ajax可以自动的为我们的请求添加cookie,也避免每次都需要去保存cookie的烦恼。。事实上,selenium为了避免其被滥用,并没有提供ajax的拦截功能。也就是说,即便我们通过js发起了请求,也不能通过selenium直接获取到返回的数据。

对此,可以利用js代码,将ajax返回的数据渲染到网页中,再通过selenium提取网页数据来获取ajax返回的数据,获取后再将网页恢复原状。这样相比于,每打开一个网页就打开一个浏览器窗口,对内存的占用就小了很多。。