puppeteer
谷歌推出的用于操作無頭瀏覽器的nodeJs框架,提供上層API用于直接操作瀏覽器,該框架適用于爬取web2.0頁面,同時對web1.0的支持率也比較高,代碼撰寫也很簡單,
puppeteer中文檔案
Puppeteer-cluster
池化思想作用于puppeteer的產物,任務分發與調度,讓nodejs可以利用自身去實作整個爬蟲,在使用該組件之前,我使用java來寫爬蟲的調度演算法,然后用eureka來呼叫nodejs的頁面抓取模塊,
puppeteer-cluster專案地址
首先需要獲取整站的所有頁面
獲取所有頁面,并確定當url是pan.baidu.com的時候過濾掉已經失效的鏈接
const {Cluster} = require('puppeteer-cluster');
const launchOptions = {
headless: true,
ignoreHTTPSErrors: true, // 忽略證書錯誤
waitUntil: 'networkidle2',
defaultViewport: {
width: 1920,
height: 1080
},
args: [
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-web-security',
'--disable-xss-auditor', // 關閉 XSS Auditor
'--no-zygote',
'--no-sandbox',
'--disable-setuid-sandbox',
'--allow-running-insecure-content', // 允許不安全內容
'--disable-webgl',
'--disable-popup-blocking',
//'--proxy-server=http://127.0.0.1:8080' // 配置代理
],
executablePath: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
};
const clusterLanuchOptions = {
concurrency: Cluster.CONCURRENCY_PAGE, // 單Chrome多tab模式
maxConcurrency: 20, // 并發的workers數
retryLimit: 2, // 重試次數
skipDuplicateUrls: true, // 不爬重復的url
monitor: true, // 顯示性能消耗
puppeteerOptions: launchOptions,
};
(async () => {
const cluster = await Cluster.launch(clusterLanuchOptions);
const cluster2 = await Cluster.launch(clusterLanuchOptions2);
await cluster2.task(async ({page, data: url}) => {
let urlTrue = url.split(' ')[0];
await page.goto(urlTrue);
await page.waitForSelector('html');
let title = await page.title();
let x = "D:\\workspace\\node\\check\\bbs\\controllers\\pantrue.txt";
if (title.indexOf('不存在') === -1) {
let valuehttps://www.cnblogs.com/chaojilaji/p/= '';
if (title.indexOf('分享無限制')) {
value = https://www.cnblogs.com/chaojilaji/p/urlTrue +' ' + title + '\n';
} else {
value = https://www.cnblogs.com/chaojilaji/p/url.split(' ')[1].substr(0, 20) + ' ' + url.split(' ')[2] + ' ' + urlTrue + ' ' + title + '\n';
}
fs.writeFile(x, value, {flag: 'a'}, function (err) {
if (err !== null) {
console.log(err);
}
});
}
});
await cluster.task(async ({page, data: url}) => {
await page.goto(url);
await page.waitForSelector('html');
let title = await page.title();
await page.content();
let x = "D:\\workspace\\node\\check\\bbs\\controllers\\pan.txt";
let y = "D:\\workspace\\node\\check\\bbs\\controllers\\outDomain.txt";
let yuanDomain = urllib.parse(urlTrue);
let newDomain = urllib.parse(url);
if (yuanDomain.hostname !== newDomain.hostname) {
if (!outUrlSet.has(newDomain.hostname)) {
fs.writeFile(y, url + ' ' + title + '\n', {
flag: 'a'
}, function (err) {
if (err) {
console.error(err);
}
});
outUrlSet.add(newDomain.hostname);
}
} else {
let links = await page.$$eval('[src],[href],[action],[data-url],[longDesc],[lowsrc]', get_src_and_href_links);
let res = await parseLinks(links, url);
console.log({links: links.length}, {res: res.length});
for (let i = 0; i < res.length; i++) {
let link = res[i];
if (link !== undefined && link.indexOf("pan.baidu.com") !== -1) {
// todo 存起來
if (!panSet.has(link)) {
fs.writeFile(x, link + ' ' + title + ' ' + url + '\n', {
flag: 'a'
}, function (err) {
if (err) {
console.error(err);
}
});
cluster2.queue(link + ' ' + title + ' ' + url);
panSet.add(link);
}
} else {
cluster.queue(link);
}
}
}
});
cluster.queue('http://www.xxxxx.com/');
await cluster.idle();
await cluster.close();
})();
async function parseLinks(links, url) {
let result = [];
// 處理url
return result;
}
function get_src_and_href_links(nodes) {
let result = [];
// 獲取節點中的所有
return result;
}
分析頁面,獲取更深的資訊
得到的盤鏈接,有些是無限制的,有些是需要tiquma的,而tiquma該如何獲取呢?往往原來的頁面里面會有,這個時候我們就需要分析原來的頁面了,
在這個網站中,提取碼幾乎都放在strong標簽中,那我們只好使用方法得到頁面中的strong標簽
在puppeteer中可以使用page.$$eval()方法來獲取strong節點集合,然后使用treeWalker遍歷整個節點得到我們想要的東西即可,
let {text,html} = await page.$$eval('strong', getTiQuMa);
function getTiQuMa(nodes) {
let html = '';
let text = '';
for (let node of nodes) {
let treeWalker = document.createTreeWalker(
node,
NodeFilter.SHOW_ELEMENT,
{
acceptNode: function (node1) {
return NodeFilter.FILTER_ACCEPT;
}
},
false
);
while (treeWalker.nextNode()) {
let element = treeWalker.currentNode;
text = text + ' ' + element.innerText;
}
}
return {text: text, html: html};
}
問題展望
現在,我們實作了基于一個網站的所有盤鏈接的獲取,那么,如果現在想要實作一個通用的盤鏈接獲取爬蟲,從而實作盤鏈接的搜索引擎該如何完成呢?
關注我,讓我給你解答,
本文由博客群發一文多發等運營工具平臺 OpenWrite 發布
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/187653.html
標籤:MySQL
上一篇:一步步學好sql陳述句
下一篇:客戶端安全相關知識點
