NodeJs Got: How to Use & Rotate Proxies
To configure NodeJs Got to use proxy, you first need to import HttpsProxyAgent
class from hpagent library. HttpsProxyAgent
is used to create a proxy agent, which is used by got
send our requests through proxy server.
To use proxy with got
, simply set agent
field inside your request options
to an object with https
attribute. https
attribute should be set to a new HttpsProxyAgent
instance initialized with proxyUrl
as proxy
param.
import got from 'got';
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent';
const requestUrl = 'https://httpbin.org/ip';
const proxyUrl = 'http://proxy.example.com:8080';
const options = {
agent: {
https: new HttpsProxyAgent({
proxy: proxyUrl
})
}
};
(async () => {
try {
const response = await got.get(requestUrl, options);
console.log(response.body);
} catch (error){
console.log('error', error);
}
})();
Note that if you're making http
requests instead of https
, you have to set options.agent.http
attribute to a new HttpProxyAgent
instance.
In this guide for The Nodejs Web Scraping Playbook, we will look at how to integrate the 3 most common types of proxies into our Nodejs Got based web scraper.
Using proxies with the Got library allows you to spread your requests over multiple IP addresses making it harder for websites to detect & block your web scrapers.
In this guide we will walk you through the 3 most common proxy integration methods and show you how to use them with Nodejs Got:
- Using Proxy IPs With Got
- Proxy Authentication With Got
- The 3 Most Common Proxy Formats
- Proxy Integration #1: Rotating Through Proxy IP List
- Proxy Integration #2: Using Proxy Gateway
- Proxy Integration #3: Using Proxy API Endpoint
Let's begin...
Need help scraping the web?
Then check out ScrapeOps, the complete toolkit for web scraping.