Proxy Port logo
如何 操作指南 > 如何为 got 设置代理

如何为 got 设置代理

got 库设置代理的最简单方法是使用 httpAgent 帮助程序包。
它可以是 hpagenthttps-proxy-agent。 我们会看看两者。

hpagent

首先安装包:

npm i hpagent
        
然后你需要将 HttpsProxyAgent 实例传递给 got config:
import got from 'got';
import { HttpsProxyAgent } from 'hpagent';

const httpsProxyAgent = new HttpsProxyAgent({proxy: 'http://127.0.0.1:8080'});

const options = { agent: { https: httpsProxyAgent } };

(async () => {
    let response = await got('https://example.com', options);
    console.log(response.body);
})();

            
        

https-proxy-agent

首先安装包:

npm i https-proxy-agent
        
然后你需要将 HttpsProxyAgent 实例传递给 got config:
import got from 'got';
import HttpsProxyAgent from 'https-proxy-agent';

const httpsProxyAgent = new HttpsProxyAgent('http://127.0.0.1:8080');

const options = { agent: { https: httpsProxyAgent } };

(async () => {
    let response = await got('https://example.com', options);
    console.log(response.body);
})();

            
        
抓取代理
了解更多