網(wǎng)絡(luò)爬蟲開發(fā)(五)01-爬蟲高級——Selenium簡介 & 根據(jù)平臺選擇安裝selenium-webdriver包 & Selenium的基本使用
第3章 爬蟲高級
學(xué)習(xí)目標(biāo):
- 使用Selenium庫爬取前端渲染的網(wǎng)頁
- 反反爬蟲技術(shù)
Selenium簡介
官方原文介紹:
Selenium automates browsers. That’s it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) be automated as well.
Selenium has the support of some of the largest browser vendors who have taken (or are taking) steps to make Selenium a native part of their browser. It is also the core technology in countless other browser automation tools, APIs and frameworks.
百度百科介紹:
Selenium [1] 是一個用于Web應(yīng)用程序測試的工具。Selenium測試直接運行在瀏覽器中,就像真正的用戶在操作一樣。支持的瀏覽器包括IE(7, 8, 9, 10, 11),[Mozilla Firefox](https://baike.baidu.com/item/Mozilla Firefox/3504923),Safari,Google Chrome,Opera等。這個工具的主要功能包括:測試與瀏覽器的兼容性——測試你的應(yīng)用程序看是否能夠很好得工作在不同瀏覽器和操作系統(tǒng)之上。測試系統(tǒng)功能——創(chuàng)建回歸測試檢驗軟件功能和用戶需求。支持自動錄制動作和自動生成 .Net、Java、Perl等不同語言的測試腳本。
簡單總結(jié):
Selenium是一個Web應(yīng)用的自動化測試框架,可以創(chuàng)建回歸測試來檢驗軟件功能和用戶需求,通過框架可以編寫代碼來啟動瀏覽器進(jìn)行自動化測試,換言之,用于做爬蟲就可以使用代碼啟動瀏覽器,讓真正的瀏覽器去打開網(wǎng)頁,然后去網(wǎng)頁中獲取想要的信息!從而實現(xiàn)真正意義上無懼反爬蟲手段!
Selenium的基本使用
- 根據(jù)平臺下載需要的webdriver
- 項目中安裝selenium-webdriver包
- 根據(jù)官方文檔寫一個小demo
根據(jù)平臺選擇webdriver
瀏覽器 | webdriver |
---|---|
Chrome | chromedriver(.exe) |
Internet Explorer | IEDriverServer.exe |
Edge | MicrosoftWebDriver.msi |
Firefox | geckodriver(.exe) |
Safari | safaridriver |
選擇版本和平臺:
下載后放入項目根目錄
安裝selenium-webdriver的包
npm i selenium-webdriver
自動打開百度搜索“黑馬程序員“
const { Builder, By, Key, until } = require('selenium-webdriver');
(async function example() {
let driver = await new Builder().forBrowser('chrome').build();
// try {
await driver.get('https://www.baidu.com');
await driver.findElement(By.id('kw')).sendKeys('黑馬程序員', Key.ENTER);
console.log(await driver.wait(until.titleIs('黑馬程序員_百度搜索'), 1000))
// } finally {
// await driver.quit();
// }
})();
實例
Selenium目錄
第一步:安包
第二步:npm i 安裝依賴
package.json
{
"name": "selenium-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"selenium-webdriver": "^4.0.0-alpha.4"
}
}
第三步:新建demo文件
helloworld.js
const { Builder, By, Key, until } = require('selenium-webdriver');
(async function example() {
let driver = await new Builder().forBrowser('chrome').build();
try {
// 自動打開百度,并搜索黑馬程序員(webdriver)
await driver.get('https://www.boxuegu.com');
// 找到元素, 向里面發(fā)送一個關(guān)鍵字并按回車 sendKeys第一個參數(shù)是搜索的關(guān)鍵詞
await driver.findElement(By.id('kw')).sendKeys('webdriver', Key.RETURN);
// 驗證是否搜索成功
// await driver.wait(until.titleIs('webdriver - Google Search'), 1000);
} finally {
// 退出
// await driver.quit();
}
})();
第四步:運行測試
node .\helloworld.js
此時,自動新開啟瀏覽器并進(jìn)行搜索文章來源:http://www.zghlxwxcb.cn/news/detail-821353.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-821353.html
到了這里,關(guān)于網(wǎng)絡(luò)爬蟲開發(fā)(五)01-爬蟲高級——Selenium簡介 & 根據(jù)平臺選擇安裝selenium-webdriver包 & Selenium的基本使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!