ScrAPI 1.0.0.3601
dotnet add package ScrAPI --version 1.0.0.3601
NuGet\Install-Package ScrAPI -Version 1.0.0.3601
<PackageReference Include="ScrAPI" Version="1.0.0.3601" />
paket add ScrAPI --version 1.0.0.3601
#r "nuget: ScrAPI, 1.0.0.3601"
// Install ScrAPI as a Cake Addin #addin nuget:?package=ScrAPI&version=1.0.0.3601 // Install ScrAPI as a Cake Tool #tool nuget:?package=ScrAPI&version=1.0.0.3601
ScrAPI SDK for .NET
This is the official .NET SDK for the ScrAPI web scraping service.
- Website
- Documentation
- Installation
- Quick Start
- Scrape Request Options
- Scrape Response Data
- Scrape Request Defaults
- Lookups
- Exceptions
- Dependency Injection
Installation
ScrAPI can be found on NuGet and can be installed by copying and pasting the following command into your Package Manager Console within Visual Studio (Tools > NuGet Package Manager > Package Manager Console).
Install-Package ScrAPI
Alternatively if you're using .NET Core then you can install ScrAPI via the command line interface with the following command:
dotnet add package ScrAPI
Quick Start
You can start scraping websites with as little as three lines of code:
var client = new ScrapiClient("YOUR_API_KEY"); // "" for limited free mode.
var request = new ScrapeRequest("https://deventerprise.com");
var response = await client.ScrapeAsync(request);
// The result will contain the content and other information about the operation.
Console.WriteLine(response?.Content);
Dependency Injection
The API client implements the interface IScrapiClient
which can be use with dependency injection and assist with mocking for unit tests.
// Add singleton to IServiceCollection
services.AddSingleton<IScrapiClient>(_ => new ScrapiClient("YOUR_API_KEY"));
Scrape Request Options
The API provides a number of options to assist with scraping a target website.
var request = new ScrapeRequest("https://deventerprise.com")
{
Cookies = new Dictionary<string, string>
{
{ "cookie1", "value1" },
{ "cookie2", "value2" },
},
Headers = new Dictionary<string, string>
{
{ "header1", "value1" },
{ "header2", "value2" },
},
ProxyCountry = "USA",
ProxyType = ProxyType.Residential,
UseBrowser = true,
SolveCaptchas = true,
RequestMethod = "GET",
ResponseFormat = ResponseFormat.Html,
CustomProxyUrl = "https://user:password@local.proxy:8080",
SessionId = Guid.NewGuid().ToString(),
CallbackUrl = new Uri("https://webhook.site/"),
};
For more detailed information on these options please refer to the documentation.
Browser Commands
When the UseBrowser
request option is used, you can supply any number of browser commands to control the browser before the resulting page state is captured.
var request = new ScrapeRequest("https://www.roboform.com/filling-test-all-fields")
{
UseBrowser = true,
AcceptDialogs = true
};
// Example of chaining commands to control the website.
request.BrowserCommands
.Input("input[name='01___title']", "Mr")
.Input("input[name='02frstname']", "Werner")
.Input("input[name='04lastname']", "van Deventer")
.Select("select[name='40cc__type']", "Discover")
.Wait(TimeSpan.FromSeconds(3))
.WaitFor("input[type='reset']")
.Click("input[type='reset']")
.Wait(TimeSpan.FromSeconds(1))
.Evaluate("console.log('any valid code...')");
Scrape Response data
The response data contains all the result information about your request including the HTML data, headers and any cookies.
var response = await client.ScrapeAsync(request);
Console.WriteLine(response.RequestUrl); // The requested URL.
Console.WriteLine(response.ResponseUrl); // The final URL of the page.
Console.WriteLine(response.Duration); // The amount of time the operation took.
Console.WriteLine(response.Attempts); // The number of attempts to scrape the page.
Console.WriteLine(response.CreditsUsed); // The number of credits used for this request.
Console.WriteLine(response.StatusCode); // The response status code from the request.
Console.WriteLine(response.Content); // The final page content.
Console.WriteLine(response.ContentHash); // SHA1 hash of the content.
Console.WriteLine(response.Html); // Html Agility Pack parsed HTML content.
foreach (var captchaSolved in response.CaptchasSolved)
{
Console.WriteLine($"{captchaSolved.Value} occurrences of {captchaSolved.Key} solved");
}
foreach (var header in response.Headers)
{
Console.WriteLine($"{header.Key}: {header.Value}");
}
foreach (var cookie in response.Cookies)
{
Console.WriteLine($"{cookie.Key}: {cookie.Value}");
}
foreach (var errorMessage in response.ErrorMessages ?? [])
{
Console.WriteLine(errorMessage); // Any errors that occurred during the request.
}
Extensions
This SDK also provides a number of convenient extensions to assist in parsing and checking the data once retrieved.
- Extract numbers only
- Strip script tags from HTML
- Safe query selector that does not throw
- Next/adjacent element finder
- Comprehensive check of element visibility.
- Style parsing.
Html Agility Pack is included as well as Hazz for HTML parsing.
Scrape Request Defaults
The SDK provides a static class to define the defaults that will be applied to every ScrapeRequest
object.
This can greatly reduce the amount of code required to create new requests if all/most of your requests need to use the same values.
// Set default that will apply to all new `ScrapeRequest` object (unless overridden).
ScrapeRequestDefaults.ProxyType = ProxyType.Residential;
ScrapeRequestDefaults.UseBrowser = true;
ScrapeRequestDefaults.SolveCaptchas = true;
ScrapeRequestDefaults.Headers.Add("Sample", "Custom-Value");
// Any new request will have the corresponding values automatically applied.
var request = new ScrapeRequest("https://deventerprise.com") { ProxyType = ProxyType.Tor };
Debug.Assert(request.ProxyType == ProxyType.Tor); // Overridden
Debug.Assert(request.UseBrowser);
Debug.Assert(request.SolveCaptchas);
Debug.Assert(request.Headers.ContainsKey("Sample"));
Lookups
The SDK provides wrappers for basic lookups such as the credit balance of an API key and a list of supported country codes to use with the ProxyCountry
request option.
Balance Check
Easily check the remaining credit balance for your API key.
var balance = await client.GetCreditBalanceAsync();
Supported Countries
var supportedCountries = await client.GetSupportedCountriesAsync();
// Use the Key value in the ProxyCountry request property.
foreach (var country in supportedCountries)
{
Console.WriteLine($"{country.Key}: {country.Name}");
}
Exceptions
Any errors using the API will always result in a ScrapiException
.
This exception also contains a property for the HTTP status that caused the exception to assist with retry logic.
var client = new ScrapiClient("YOUR_API_KEY"); // "" for limited free mode.
var request = new ScrapeRequest("https://deventerprise.com");
try
{
var result = await client.ScrapeAsync(request);
Console.WriteLine(result?.Content);
}
catch (ScrapiException ex) when (ex.StatusCode == System.Net.HttpStatusCode.InternalServerError)
{
// Error messages from the server aim to be as helpful as possible.
Console.WriteLine(ex.Message);
throw;
}
// The result will contain the content and other information about the operation.
Console.WriteLine(result?.Content);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Fizzler (>= 1.3.1)
- Fizzler.Systems.HtmlAgilityPack (>= 1.2.1)
- HtmlAgilityPack (>= 1.11.72)
- Newtonsoft.Json (>= 13.0.3)
-
.NETStandard 2.1
- Fizzler (>= 1.3.1)
- Fizzler.Systems.HtmlAgilityPack (>= 1.2.1)
- HtmlAgilityPack (>= 1.11.72)
- Newtonsoft.Json (>= 13.0.3)
-
net6.0
- Fizzler (>= 1.3.1)
- Fizzler.Systems.HtmlAgilityPack (>= 1.2.1)
- HtmlAgilityPack (>= 1.11.72)
- Newtonsoft.Json (>= 13.0.3)
-
net8.0
- Fizzler (>= 1.3.1)
- Fizzler.Systems.HtmlAgilityPack (>= 1.2.1)
- HtmlAgilityPack (>= 1.11.72)
- Newtonsoft.Json (>= 13.0.3)
-
net9.0
- Fizzler (>= 1.3.1)
- Fizzler.Systems.HtmlAgilityPack (>= 1.2.1)
- HtmlAgilityPack (>= 1.11.72)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0.3601 | 387 | a month ago |
1.0.0.3546 | 533 | 2 months ago |
0.6.0.3466 | 550 | 2 months ago |
0.6.0.3431 | 1,552 | 4 months ago |
0.6.0.3362 | 4,637 | 7 months ago |
0.5.0.3327 | 515 | 8 months ago |
0.5.0.3308 | 984 | 9 months ago |
0.5.0.3250 | 1,052 | 5/1/2024 |
0.5.0.3220 | 1,107 | 4/3/2024 |
0.5.0.3137 | 768 | 2/24/2024 |
0.5.0.3060 | 300 | 2/2/2024 |
0.4.30.3031 | 190 | 1/30/2024 |
0.3.25.2995 | 198 | 1/25/2024 |
0.3.23.2975 | 168 | 1/23/2024 |
0.2.1130.2205 | 9,675 | 11/30/2022 |
0.2.1016.2815 | 150 | 10/16/2023 |
0.2.823.2644 | 181 | 8/26/2023 |
0.2.506.1670 | 2,378 | 5/6/2022 |
0.2.505.1669 | 433 | 5/5/2022 |
0.2.505.1667 | 462 | 5/5/2022 |
0.2.413.1509 | 598 | 4/13/2022 |
0.1.1396.24 | 505 | 3/28/2022 |
0.1.1351.21 | 487 | 2/3/2022 |
0.1.1336.18 | 471 | 1/17/2022 |
0.1.1324.16 | 308 | 1/3/2022 |
0.1.1310.13 | 364 | 11/15/2021 |
0.1.1300.11 | 331 | 11/3/2021 |
0.0.1286.8 | 360 | 10/22/2021 |
0.0.1280.5 | 358 | 10/21/2021 |
0.0.1275.4 | 337 | 10/21/2021 |
0.0.1268.3 | 427 | 10/20/2021 |