Home > Web Front-end > JS Tutorial > How to capture proxy IP in node.js?

How to capture proxy IP in node.js?

零下一度
Release: 2017-05-03 10:03:03
Original
1610 people have browsed it

This article mainly introduces the relevant information about node.js grabbing proxy ip example code. Friends in need can refer to it

node.js implements grabbing proxy ip

Main file: 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= $(&#39;#list table tbody&#39;).find(&#39;tr&#39;).eq(i).find(&#39;td&#39;).eq(0).text() ;
    let port = $(&#39;#list table tbody&#39;).find(&#39;tr&#39;).eq(i).find(&#39;td&#39;).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(&#39;ok&#39;);
    }, time);
  })
};
const pageNumber=10;
var start = async function(){
  for(let j=1;j<pageNumber;j++){
     console.log(`当前是第${j}次等待..`);
    fetchUrl(`http://www.kuaidaili.com/free/inha/${j}/`);
    await sleep(1500);
  }
}
start();
Copy after login

Package support: 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"
 }
}
Copy after login

The mongodb database needs to be installed locally for Store the captured IP. IP verification is not yet implemented. I write this mainly out of curiosity.

The above code can capture the IP of the IP proxy website and store it in the mongodb database.

The following is a simple server implementation based on the koa2 api interface

server

const Promise=require(&#39;bluebird&#39;);
let mongoose=require(&#39;mongoose&#39;);
const koa=require(&#39;koa&#39;);
const app=new koa();
var router = require(&#39;koa-router&#39;)();
Promise.promisifyAll(mongoose);
let Schema=mongoose.Schema;
mongoose.connect(&#39;mongodb://localhost:27017/ipproxypool&#39;);
let IPpool=new Schema({
  ip:{type:String,unique:true}
})
let Ipproxy=mongoose.model(&#39;IP&#39;,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 = &#39;text/json&#39;;
 ctx.response.body = map;
});
app.listen(3000);
console.log(&#39;server listen:3000&#39;)
Copy after login

As for why there is both promise and async, it is because I’m not very familiar with asynchronous syntax, so how do I know how to write it?

Usage:

According to package.json

npm install //Installation support

node index.js //Get Proxy ip

node server.js //Run simple ip interface

The above is the detailed content of How to capture proxy IP in node.js?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template