68 lines
2.4 KiB
JavaScript
68 lines
2.4 KiB
JavaScript
import * as cheer from "cheerio";
|
||
import axios from "axios";
|
||
import { HttpsProxyAgent } from "https-proxy-agent";
|
||
import Config from "../config/config.js";
|
||
|
||
class utils {
|
||
constructor() {
|
||
this.ax = axios
|
||
// 让 ajax 请求 走flclash的翻墙代理
|
||
const httpsAgent = new HttpsProxyAgent(`http://127.0.0.1:${Config.proxyPort}`);
|
||
this.ax.defaults.httpsAgent = httpsAgent;
|
||
this.ax.defaults.proxy = false;
|
||
|
||
this.ResponseInterceptor()
|
||
}
|
||
|
||
/**
|
||
* 返回 cheerio 数据结构
|
||
* @param html
|
||
* @returns {*}
|
||
*/
|
||
cheerio(html) {
|
||
return cheer.load(html)
|
||
}
|
||
|
||
split(str, index = 1) {
|
||
return str.split(Config.baseUrl)[index]
|
||
}
|
||
|
||
/**
|
||
*
|
||
* @param url 请求的地址
|
||
* @returns {Promise<AxiosResponse<any>>}
|
||
*/
|
||
async get(url) {
|
||
return await this.ax.get(url, {
|
||
headers: {
|
||
'cookie': `_ga=GA1.1.2076335189.1731510827; history_search=%5B%22%5Cu6768%5Cu6668%5Cu6668%26type%3Dpost%22%2C%22%5Cu767d%5Cu4e1d%26type%3Dpost%22%2C%22jk%26type%3Dpost%22%5D; PHPSESSID=22oh5maqqirjstqs1p7gbtome7; _lscache_vary=0e8aace2958628a1edb01710bbabbcab; showed_system_notice=showed; _ga_ZT9Q6FCC5R=GS1.1.1732278002.10.1.1732278777.0.0.0`,
|
||
'referer': 'https://www.06se.com/',
|
||
'user-agent': `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36`,
|
||
'accept': `text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7`
|
||
}
|
||
});
|
||
}
|
||
|
||
/**
|
||
*
|
||
* 响应拦截器
|
||
* @constructor
|
||
*/
|
||
async ResponseInterceptor() {
|
||
this.ax.interceptors.response.use((response) => {
|
||
return this.cheerio(response.data);
|
||
}, (error) => {
|
||
console.log(`
|
||
❌ 请求错误❕❕❕
|
||
1: 错误信息 ${error.message}
|
||
2: 原因:目标网站禁止国内大陆IP访问,爬虫会走系统代理进行请求访问
|
||
但检测到你未开启翻墙工具,请打开Clash或FlClash等代理工具,保证能正常访问 Google。
|
||
|
||
3: 自定义代理端口:修改代码'/config/config.js'中 proxyPort 实现自定义端口,端口从代理工具中查看
|
||
`);
|
||
return Promise.reject(error)
|
||
})
|
||
}
|
||
}
|
||
|
||
export default new utils(); |