溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

node.js如何抓取代理ip

發(fā)布時(shí)間:2021-07-20 14:35:43 來源:億速云 閱讀:165 作者:小新 欄目:web開發(fā)

這篇文章主要介紹node.js如何抓取代理ip,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

node.js實(shí)現(xiàn)抓取代理ip

主要文件:index.js

/*
* 支持:node.js v7.9.0
*/
const cheerio=require('cheerio');
const fetch =require('node-fetch');
const Promise=require('bluebird');
let mongoose=require('mongoose');

Promise.promisifyAll(mongoose);
let Schema=mongoose.Schema;
mongoose.connect('mongodb://localhost:27017/ipproxypool');
let IPpool=new Schema({
  ip:{type:String,unique:true}
})
let Ipproxy=mongoose.model('IP',IPpool);

function fetchUrl(url){
  fetch(url,{
    method:'get',
    headers:{
    }
  })
  .then(res=>res.text())
  .then(body=>{
    let $=cheerio.load(body);
    let length=$('#list table tbody').find('tr').length;
    for (let i=0;i<length;i++){
    let ipaddress= $('#list table tbody').find('tr').eq(i).find('td').eq(0).text() ;
    let port = $('#list table tbody').find('tr').eq(i).find('td').eq(1).text();
    console.log(`IP:${ipaddress}:${port}`);
    let ip=`${ipaddress}:${port}`
    let ippool=new Ipproxy({
      ip:ip
    })
    ippool.save();
    }
  })
}

var sleep = function (time) {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      resolve('ok');
    }, time);
  })
};
const pageNumber=10;
var start = async function(){
  for(let j=1;j<pageNumber;j++){
     console.log(`當(dāng)前是第${j}次等待..`);
    fetchUrl(`http://www.kuaidaili.com/free/inha/${j}/`);
    await sleep(1500);
  }
}
start();

包支持 : package.json

{
 "name": "demo-4-ipproxypool",
 "version": "1.0.0",
 "description": "",
 "main": "index.js",
 "scripts": {
  "test": "echo \"Error: no test specified\" && exit 1"
 },
 "author": "false-l",
 "license": "",
 "devDependencies": {
  "babel-preset-es2015": "^6.24.1",
  "babel-preset-react": "^6.24.1",
  "babel-preset-stage-3": "^6.24.1"
 },
 "dependencies": {
  "babel-core": "^6.24.1",
  "bluebird": "^3.5.0",
  "cheerio": "^0.22.0",
  "koa": "^2.2.0",
  "koa-router": "^7.1.1",
  "mongoose": "^4.9.6",
  "node-fetch": "^1.6.3"
 }
}

本地需要安裝mongodb數(shù)據(jù)庫,用于存儲(chǔ)抓取到的ip,目前還未實(shí)現(xiàn)ip驗(yàn)證。寫這個(gè)主要是處于好奇。

上面的代碼就可以實(shí)現(xiàn)抓取ip代理網(wǎng)站的ip并存到mongodb數(shù)據(jù)庫中。

下面在放出一個(gè)基于koa2的api接口的簡易服務(wù)器實(shí)現(xiàn)

server

const Promise=require('bluebird');
let mongoose=require('mongoose');
const koa=require('koa');
const app=new koa();
var router = require('koa-router')();
Promise.promisifyAll(mongoose);
let Schema=mongoose.Schema;
mongoose.connect('mongodb://localhost:27017/ipproxypool');
let IPpool=new Schema({
  ip:{type:String,unique:true}
})
let Ipproxy=mongoose.model('IP',IPpool);

app.use(async (ctx, next) => {
 await next();
 var data=await Ipproxy.find({},function(err,ips){
  var ipmap=[];
   ips.forEach(function(ip){
     ipmap[ip._id]=ip;
     //console.log(ip)
   });
 })
 var map=data.map(ip=>ip.ip);
 ctx.response.type = 'text/json';
 ctx.response.body = map;
});
app.listen(3000);
console.log('server listen:3000')

至于為什么既有promise又有async,是因?yàn)閷Ξ惒秸Z法還不是很熟,怎么會(huì)怎么寫了。

使用方式:

 根據(jù)package.json

npm install   // 安裝支持

node index.js  //獲取代理 ip

node server.js  //運(yùn)行簡易ip接口

以上是“node.js如何抓取代理ip”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI