gpt4 book ai didi

javascript - 如何在 Node.js 中使用代理

转载 作者:行者123 更新时间:2023-12-01 01:55:11 24 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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){
...
})

关于javascript - 如何在 Node.js 中使用代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51093406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com