|
1 | 1 | # ScrapingAnt API client for Python |
| 2 | +`scrapingant-client` is the official library to access [ScrapingAnt API](https://docs.scrapingant.com) from your |
| 3 | +Python applications. It provides useful features like parameters encoding to improve the ScrapingAnt usage experience. |
| 4 | + |
| 5 | +<!-- toc --> |
| 6 | + |
| 7 | +- [Quick Start](#quick-start) |
| 8 | +- [API key](#api-key) |
| 9 | +- [Retries with exponential backoff](#retries-with-exponential-backoff) |
| 10 | +- [API Reference](#api-reference) |
| 11 | +- [Examples](#examples) |
| 12 | + |
| 13 | +<!-- tocstop --> |
| 14 | + |
| 15 | +## Quick Start |
| 16 | +```python3 |
| 17 | +from scrapingant_client.client import ScrapingAntClient |
| 18 | + |
| 19 | +client = ScrapingAntClient(token='<YOUR-SCRAPINGANT-API-TOKEN>') |
| 20 | +# Scrape the example.com site. |
| 21 | +result = client.general_request('https://example.com') |
| 22 | +print(result.content) |
| 23 | +``` |
| 24 | + |
| 25 | +## API key |
| 26 | +In order to get API key you'll need to register at [ScrapingAnt Service](https://app.scrapingant.com) |
| 27 | + |
| 28 | +## API Reference |
| 29 | +All public classes, methods and their parameters can be inspected in this API reference. |
| 30 | + |
| 31 | +<a name="ScrapingAntClient"></a> |
| 32 | + |
| 33 | +#### [](#ScrapingAntClient) `ScrapingAntClient(token)` |
| 34 | + |
| 35 | + |
| 36 | +| Param | Type | Default | |
| 37 | +| --- | --- | --- | |
| 38 | +| [token] | <code>string</code> | | |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +* * * |
| 43 | + |
| 44 | +<a name="ScrapingAntClient+scrape"></a> |
| 45 | + |
| 46 | +#### [](#ScrapingAntClient+scrape) `ScrapingAntClient.general_request(url, cookies, js_snippet, proxy_country, return_text)` ⇒ [<code>ScrapingAnt API response</code>](https://docs.scrapingant.com/request-response-format#response-structure) |
| 47 | + |
| 48 | +https://docs.scrapingant.com/request-response-format#available-parameters |
| 49 | + |
| 50 | +| Param | Type | Default | |
| 51 | +| --- | --- | --- | |
| 52 | +| url | <code>string</code> | | |
| 53 | +| cookies | <code>string</code> | None | |
| 54 | +| js_snippet | <code>string</code> | None | |
| 55 | +| proxy_country | <code>string</code> | None | |
| 56 | +| return_text | <code>boolean</code> | False | |
| 57 | + |
| 58 | +**IMPORTANT NOTE:** <code>js_snippet</code> will be encoded to Base64 automatically by the ScrapingAnt client library. |
| 59 | + |
| 60 | +* * * |
| 61 | + |
| 62 | +<a name="ScrapingAntApiError"></a> |
| 63 | + |
| 64 | +### [](#ScrapingantClientException) ScrapingantClientException |
| 65 | + |
| 66 | +`ScrapingantClientException` is base Exception class, used for all errors |
| 67 | + |
| 68 | +* * * |
| 69 | + |
| 70 | +## Examples |
| 71 | + |
| 72 | +### Sending custom cookies |
| 73 | + |
| 74 | +```python3 |
| 75 | +from scrapingant_client.client import ScrapingAntClient |
| 76 | +from scrapingant_client.cookie import Cookie |
| 77 | + |
| 78 | +client = ScrapingAntClient(token='<YOUR-SCRAPINGANT-API-TOKEN>') |
| 79 | + |
| 80 | +result = client.general_request( |
| 81 | + 'https://httpbin.org/cookies', |
| 82 | + cookies=[ |
| 83 | + Cookie(name='cookieName1', value='cookieVal1'), |
| 84 | + Cookie(name='cookieName2', value='cookieVal2'), |
| 85 | + ] |
| 86 | +) |
| 87 | +print(result.content) |
| 88 | +# Response cookies is a list of Cookie objects |
| 89 | +# They can be used in next requests |
| 90 | +response_cookies = result.cookies |
| 91 | +``` |
| 92 | + |
| 93 | +### Executing custom JS snippet |
| 94 | + |
| 95 | +```python |
| 96 | +from scrapingant_client.client import ScrapingAntClient |
| 97 | +client = ScrapingAntClient(token='<YOUR-SCRAPINGANT-API-TOKEN>') |
| 98 | + |
| 99 | +customJsSnippet = """ |
| 100 | +var str = 'Hello, world!'; |
| 101 | +var htmlElement = document.getElementsByTagName('html')[0]; |
| 102 | +htmlElement.innerHTML = str; |
| 103 | +""" |
| 104 | +result = client.general_request( |
| 105 | + 'https://example.com', |
| 106 | + js_snippet=customJsSnippet, |
| 107 | +) |
| 108 | +print(result.content) |
| 109 | +``` |
0 commit comments