作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试抓取网站“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/
我是一名优秀的程序员,十分优秀!