Nodejs实现爬虫的几种方式
获取代理 IP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // 需要安装 axios 模块 - npm install axios --save const axios = require( 'axios' ) // id secret 等参数是在猿人云官网提取API内获取的 const queries = { id: 'xxx' , secret: 'xxx' , limit: 1, format: 'txt' , auth_mode: 'auto' }; axios.get( 'http://tunnel-api.apeyun.com/q' , { params: queries, }).then((response) => { console.log( 'IP:' , response.data); }). catch ((e) => { console.error( 'Error:' , e); }); |
爬虫程序
-
axios
123456789101112131415161718192021222324252627282930313233343536// 需要安装 axios 模块 - npm install axios --save
const axios = require(
'axios'
)
// 要访问的目标页面
let
targetUrl =
"http://www.baidu.com"
// 代理服务器,假设提取到的代理 ip 是 123.123.123.123:1234
const proxyHost =
"123.123.123.123"
const proxyPort = 1234
// 代理验证信息(猿人云官网获取)
const proxyUser =
"xxx"
const proxyPass =
"xxx"
let
proxy = {
host: proxyHost,
port: proxyPort,
auth: {
username: proxyUser,
password: proxyPass
}
}
// 参见官方文档 https://github.com/axios/axios#request-config
axios.get(targetUrl,{proxy:proxy})
.then(
function
(response) {
// handle success
console.log(response.data)
})
.
catch
(
function
(error) {
// handle error
console.log(error)
})
.finally(
function
() {
// always executed
});
-
http
-
1234567891011121314151617181920212223242526272829303132333435
const http = require(
"http"
)
const url = require(
"url"
)
// 要访问的目标页面
const targetUrl =
"http://www.baidu.com"
const urlParsed = url.parse(targetUrl)
// 代理服务器,假设提取到的代理ip是123.123.123.123:1234
const proxyHost =
"123.123.123.123"
const proxyPort =
"1234"
// 代理隧道验证信息(猿人云官网获取)
const proxyUser =
"xxx"
const proxyPass =
"xxx"
const base64 = Buffer.from(proxyUser +
":"
+ proxyPass).toString(
"base64"
)
const options = {
host: proxyHost,
port: proxyPort,
path: targetUrl,
method:
"GET"
,
headers: {
"Host"
: urlParsed.hostname,
"Proxy-Authorization"
:
"Basic "
+ base64
}
}
http.request(options,
function
(res) {
console.log(
"got response: "
+ res.statusCode)
})
.on(
"error"
,
function
(err) {
console.log(err)
})
.end()
-
request
-
123456789101112131415161718192021222324252627282930
// 需要安装 request 模块 - npm install request --save
const request = require(
"request"
)
// 要访问的目标页面
const targetUrl =
"http://www.baidu.com"
// 代理服务器,假设提取到的代理ip是123.123.123.123:1234
const proxyHost =
"123.123.123.123"
const proxyPort = 1234
// 代理隧道验证信息(猿人云官网获取)
const proxyUser =
"xxx"
const proxyPass =
"xxx"
const proxyUrl =
"http://"
+ proxyUser +
":"
+ proxyPass +
"@"
+ proxyHost +
":"
+ proxyPort
const proxiedRequest = request.defaults({
'proxy'
: proxyUrl})
const options = {
url: targetUrl,
headers: {}
}
proxiedRequest.get(options,
function
(err, res, body) {
console.log(
"got response: "
+ res.statusCode)
console.log(
"got response: "
+ body)
})
.on(
"error"
,
function
(err) {
console.log(err);
})
-
superagent
-
123456789101112131415161718192021222324
// 需要安装 superagent 和 superagent-proxy 模块 - npm install superagent superagent-proxy --save
const request = require(
"superagent"
)
require(
"superagent-proxy"
)(request)
// 要访问的目标页面
const targetUrl =
"http://www.baidu.com"
// 代理服务器,假设提取到的代理ip是123.123.123.123:1234
const proxyHost =
"123.123.123.123"
;
const proxyPort = 1234;
// 代理隧道验证信息(猿人云官网获取)
const proxyUser =
"xxx"
const proxyPass =
"xxx"
const proxyUrl =
"http://"
+ proxyUser +
":"
+ proxyPass +
"@"
+ proxyHost +
":"
+ proxyPort
request.get(targetUrl).proxy(proxyUrl).end(
function
onResponse(err, res) {
if
(err) {
return
console.log(err)
}
console.log(res.status, res.headers)
console.log(res.text)
});
使用 node 运行这个文件,当你的控制台打印出一大段 HTML 代码说明这个爬虫程序成功了
转自:https://mp.weixin.qq.com/s/JA11NzbbHtKqgijmdmJPlw
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2019-03-28 关于在scrapy中使用xpath
2019-03-28 scrapy爬取虎嗅(典型的post请求在scrapy中的应用)