如何在Node.js中使用代理

时间:2018-06-29 02:14:24

标签: javascript node.js web-scraping proxy

我正在尝试抓取网站“ https://shmoti.com”,但似乎他们阻止了我的IP。所以我正在尝试将代理与node.js一起使用。

我正在使用node-fetch模块来获取网站的html正文。

如何对node-fetch使用代理?我也不知道从哪里可以得到这样的免费代理。有什么帮助吗?

我需要使用 fetch 模块本身,因为我正在执行所有异步处理方式。

1 个答案:

答案 0 :(得分:1)

https://github.com/bitinn/node-fetch#options

从他们的文档中得知

{
// These properties are part of the Fetch Standard
method: 'GET',
headers: {},        // request headers. format is the identical to that accepted by the Headers constructor (see below)
body: null,         // request body. can be null, a string, a Buffer, a Blob, or a Node.js Readable stream
redirect: 'follow', // set to `manual` to extract redirect headers, `error` to reject redirect

// The following properties are node-fetch extensions
follow: 20,         // maximum redirect count. 0 to not follow redirect
timeout: 0,         // req/res timeout in ms, it resets on redirect. 0 to disable (OS limit applies)
compress: true,     // support gzip/deflate content encoding. false to disable
size: 0,            // maximum response body size in bytes. 0 to disable
agent: null         // http(s).Agent instance, allows custom proxy, certificate, lookup, family etc.

}

所以您可以尝试

fetch('example.com',{
        agent: new HttpsProxyAgent('http://127.0.0.1:8580')
    }).then(function(res){
    ...
})