Puppeteer-Sharp is a .NET port of the popular Node library Puppeteer which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. Puppeteer-Sharp is used for browser automation, scraping web pages, and generating screenshots or PDFs of web content.
In Puppeteer-Sharp, you can configure a proxy server for your browsing context. To do this, you can pass proxy server details when launching a new browser instance using the LaunchOptions
class.
Here is an example of how you can configure a proxy in Puppeteer-Sharp:
using PuppeteerSharp;
class Program
{
static async Task Main(string[] args)
{
// Set up the launch options with the proxy server configuration
var launchOptions = new LaunchOptions
{
Headless = true, // Set to false if you want to see the browser
Args = new string[] { "--proxy-server=127.0.0.1:8080" } // Replace with your proxy address
};
// Launch the browser with the specified options
using (var browser = await Puppeteer.LaunchAsync(launchOptions))
{
// Create a new page
using (var page = await browser.NewPageAsync())
{
// Navigate to the desired URL
await page.GoToAsync("http://example.com");
// Perform actions on the page
// ...
// Close the browser
await browser.CloseAsync();
}
}
}
}
In the code example above, we are specifying the proxy server by adding the --proxy-server
argument to the browser launch options. Replace 127.0.0.1:8080
with the IP address and port of your proxy server.
Please note that in the context of web scraping, using proxies can be essential to prevent IP blocking by the target website. However, ensure that you are in compliance with the website's terms of service and applicable laws when scraping content.
If you need to use authentication with your proxy, you may need to handle the authentication event by listening to the Page.Auth
event and providing the credentials:
page.Auth += async (sender, e) =>
{
await e.AuthenticateAsync(new Credentials
{
Username = "your-username",
Password = "your-password"
});
};
Remember to install the Puppeteer-Sharp package via NuGet before running the code:
dotnet add package PuppeteerSharp
Finally, keep in mind that Puppeteer-Sharp is a .NET library, and the setup and usage may change with newer versions. Always refer to the official documentation or GitHub repository for the latest information and best practices.