先记录几个比较可靠的代理IP平台:
先定义targetUrl和proxyUrl如下
const targetUrl = 'https://www.baidu.com/' const proxyUrl = 'http://proxyUser:proxyPass@proxyHost:proxyPort' // const proxyUrl = 'http://127.0.0.1:9527'
request中使用
// 官方文档 https://github.com/request/request#readme const request = require('request') request.get({ url: targetUrl, headers: {}, proxy: proxyUrl, timeout: 10000 }, function (error, response, body) { console.log(error) console.log(response) })
superagent中使用
// 官方文档 https://github.com/TooTallNate/superagent-proxy#readme const request = require('superagent') require('superagent-proxy')(request) // ... const res = await request('GET', targetUrl).proxy(proxyUrl).timeout(10000) console.log(res) // ...
axios中使用
// 官方文档 https://github.com/axios/axios#request-config const axios = require('axios') // ... const res = await axios.get(targetUrl, { proxy: { host: 'proxyHost', port: 'proxyPort', auth: { username: 'proxyUser', password: 'proxyPass' } } }) console.log(res) // ...
其他库使用代理的方法可参考他们各自的官方文档或:
有朋自远方来...评论一下呗O(∩_∩)O