Do you know how to use OxyLabs Real-time Crawler for web pages? This is the most comprehensive introduction from OxyLabs official.
Quick Start
HTML Crawler API is built to help you in your heavy-duty data retrieval operations. You can use HTML Crawler API to access various public pages. It enables effortless web data extraction without any delays or errors.
HTML Crawler API uses basic HTTP authentication that requires sending username and password.
This is by far the fastest way to start using HTML Crawler API. You will make a request to https://ip.oxylabs.io
using Realtime integration method. Don't forget to replace USERNAME
和 PASSWORD
with your proxy user credentials.
curl --user "USERNAME:PASSWORD" 'https://realtime.oxylabs.io/v1/queries' -H "Content-Type: application/json" -d '{"source": "universal", "url": "https://ip.oxylabs.io"}'
If you have any questions not covered by this documentation, please contact your account manager or our support staff at [email protected].
Integration Methods
HTML Crawler API supports three integration methods which have their unique benefits:
- Push-Pull. Using this method it is now required to mainain an active connection with our endpoint to retrieve the data. Upon making a request, our system is able to automatically ping users server when the job is done (see Callback). This method saves computing resources and can be scaled easily.
- Realtime. The method requires user to maintain an active connection with our endpoint in order to get the results successfully when the job is completed. This method can be implemented into one service while Push-Pull method is a two step process.
- SuperAPI. This method is very similar to Realtime but instead posting data to our endpoint, user can use HTML Cralwer as a proxy. To retrieve the data, user must set up a proxy endpoint and make GET request to a desired URL. Additional parameters must be added using headers.
Our recommended data extraction method is Push-Pull.
Push-Pull
This is the most simple yet the most reliable and recommended data delivery method. In Push-Pull scenario you send us a query, we return you job id
, and once the job is done you can use that id
to retrieve content from /results
endpoint. You can check job completion status yourself, or you can set up a simple listener that is able to accept POST queries.
This way, we will send you a callback message once the job is ready to be retrieved. In this particular example the results will be automatically uploaded to your S3 bucket named YOUR_BUCKET_NAME
.
Single Query
The following endpoint will handle single queries for one keyword or URL. The API will return a confirmation message containing job information, including job id
. You can check job completion status using that id
, or you can ask us to ping your callback endpoint once the scraping task is finished by adding callback_url
in the query.
POST https://data.oxylabs.io/v1/queries
You need to post query parameters as data in the JSON body.
curl --user user:pass1\ 'https://data.oxylabs.io/v1/queries' \ -H "Content-Type: application/json" \ -d '{"source": "universal", "url": "https://stackoverflow.com/questions/tagged/python", "callback_url": "https://your.callback.url", "storage_type": "s3", "storage_url": "YOUR_BUCKET_NAME"}'
The API will respond with query information in JSON format, by printing it in the response body, similar to this:
{ "callback_url": "https://your.callback.url", "client_id": 5, "created_at": "2019-10-01 00:00:01", "domain": "com", "geo_location": null, "id": "12345678900987654321", "limit": 10, "locale": null, "pages": 1, "parse": false, "render": null, "url": "https://stackoverflow.com/questions/tagged/python", "source": "universal", "start_page": 1, "status": "pending", "storage_type": "s3", "storage_url": "YOUR_BUCKET_NAME/12345678900987654321.json", "subdomain": "www", "updated_at": "2019-10-01 00:00:01", "user_agent_type": "desktop", "_links": [ { "rel": "self", "href": "http://data.oxylabs.io/v1/queries/12345678900987654321", "method": "GET" }, { "rel": "results", "href": "http://data.oxylabs.io/v1/queries/12345678900987654321/results", "method": "GET" } ] }
Check Job Status
If your query had a callback_url
, we will send you a message containing a link to the content once the scraping task is done. However, if there was no callback_url
in the query, you will need to check the job status yourself. For that, you need to use the URL in href
under rel
:self
in the response message you received after submitting your query to our API. It should look similar to this: http://data.oxylabs.io/v1/queries/12345678900987654321
.
GET https://data.oxylabs.io/v1/queries/{id}
Querying this link will return the job information, including its status
. There are three possible status
values:
pending |
The job is still in the queue and has not been completed. |
done |
The job is completed, you may retrieve the result by querying the URL in href under rel :results : http://data.oxylabs.io/v1/queries/12345678900987654321/results |
faulted |
There was an issue with the job, and we could not complete it, most likely due to a server error on the target site's side. |
curl --user user:pass1 'http://data.oxylabs.io/v1/queries/12345678900987654321'
The API will respond with query information in JSON format, by printing it in the response body. Notice that job status
has been changed to done
. You can now retrieve content by querying http://data.oxylabs.io/v1/queries/12345678900987654321/results
.
You can also see that the task has been updated_at
2019-10-01 00:00:15
– the query took 14 seconds to complete.
{ "client_id": 5, "created_at": "2019-10-01 00:00:01", "domain": "com", "geo_location": null, "id": "12345678900987654321", "limit": 10, "locale": null, "pages": 1, "parse": false, "render": null, "url": "sofa", "source": "universal", "start_page": 1, "status": "done", "subdomain": "www", "updated_at": "2019-10-01 00:00:15", "user_agent_type": "desktop", "_links": [ { "rel": "self", "href": "http://data.oxylabs.io/v1/queries/12345678900987654321", "method": "GET" }, { "rel": "results", "href": "http://data.oxylabs.io/v1/queries/12345678900987654321/results", "method": "GET" } ] }
Retrieve Job Content
Once you know the job is ready to be retrieved by checking its status, you can GET it using the URL in href
under rel
:results
in our initial response. It should look similar to this: http://data.oxylabs.io/v1/queries/12345678900987654321/results
.
GET https://data.oxylabs.io/v1/queries/{id}/results
The results can be automatically retrieved without periodically checking job status by setting up Callback service. User needs to specfy the IP or domain of the server where the Callback service is running. When our system completes a job, it will send a message to the provided IP or domain and the Callback service will download the results as described in the Callback implementation example.
curl --user user:pass1 'http://data.oxylabs.io/v1/queries/12345678900987654321/results'
The API will return job content:
{ "results": [ { "content": "<!doctype html> CONTENT ", "created_at": "2019-10-01 00:00:01", "updated_at": "2019-10-01 00:00:15", "page": 1, "url": "https://stackoverflow.com/questions/tagged/python", "job_id": "12345678900987654321", "status_code": 200 } ] }
Callback
A callback is a 职位
request we send to your machine, informing that the data extraction task is completed and providing URL to download scraped content. This means that you no longer need to check job status manually. Once the data is here, we will let you know, and all you need to do now is retrieve it.
# Please see the code samples in Python and PHP.
Sample callback output
{ "created_at":"2019-10-01 00:00:01", "updated_at":"2019-10-01 00:00:15", "locale":null, "client_id":163, "user_agent_type":"desktop", "source":"universal", "pages":1, "subdomain":"www", "status":"done", "start_page":1, "parse":0, "render":null, "priority":0, "ttl":0, "origin":"api", "persist":true, "id":"12345678900987654321", "callback_url":"http://your.callback.url/", "url":"https://stackoverflow.com/questions/tagged/python", "domain":"de", "limit":10, "geo_location":null, {...} "_links":[ { "href":"https://data.oxylabs.io/v1/queries/12345678900987654321", "method":"GET", "rel":"self" }, { "href":"https://data.oxylabs.io/v1/queries/12345678900987654321/results", "method":"GET", "rel":"results" } ], }
Batch Query
HTML Crawler API also supports executing multiple keywords, up to 1,000 keywords with each batch. The following endpoint will submit multiple keywords to the extraction queue.
POST https://data.oxylabs.io/v1/queries/batch
You need to post query parameters as data in the JSON body.
The system will handle every keyword as a separate request. If you provided a callback URL, you will get a separate call for each keyword. Otherwise, our initial response will contain job id
s for all keywords. For example, if you sent 50 keywords, we will return 50 unique job id
s.
Important! 询问
is the only parameter that can have multiple values. All other parameters are the same for that batch query.
curl --user user:pass1 'https://data.oxylabs.io/v1/queries/batch' -H 'Content-Type: application/json' \ -d '@keywords.json'
keywords.json
content:
{ "url":[ "https://stackoverflow.com/questions/tagged/python", "https://stackoverflow.com/questions/tagged/golang", "https://stackoverflow.com/questions/tagged/php" ], "source": "universal", "callback_url": "https://your.callback.url" }
The API will respond with query information in JSON format, by printing it in the response body, similar to this:
{ "queries": [ { "callback_url": "https://your.callback.url", {...} "created_at": "2019-10-01 00:00:01", "domain": "com", "id": "12345678900987654321", {...} "url": "https://stackoverflow.com/questions/tagged/python", "source": "universal", {...} "rel": "results", "href": "http://data.oxylabs.io/v1/queries/12345678900987654321/results", "method": "GET" } ] }, { "callback_url": "https://your.callback.url", {...} "created_at": "2019-10-01 00:00:01", "domain": "com", "id": "12345678901234567890", {...} "url": "https://stackoverflow.com/questions/tagged/golang", "source": "universal", {...} "rel": "results", "href": "http://data.oxylabs.io/v1/queries/12345678901234567890/results", "method": "GET" } ] }, { "callback_url": "https://your.callback.url", {...} "created_at": "2019-10-01 00:00:01", "domain": "com", "id": "01234567899876543210", {...} "url": "https://stackoverflow.com/questions/tagged/php", "source": "universal", {...} "rel": "results", "href": "http://data.oxylabs.io/v1/queries/01234567899876543210/results", "method": "GET" } ] } ] }
Get Notifier IP Address List
You may want to whitelist the IPs sending you callback messages or get the list of these IPs for other purposes. This can be done by 获取
ing this endpoint: https://data.oxylabs.io/v1/info/callbacker_ips
.
curl --user user:pass1 'https://data.oxylabs.io/v1/info/callbacker_ips'
The API will return the list of IPs making callback requests to your system:
{ "ips": [ "x.x.x.x", "y.y.y.y" ] }
Upload to Storage
By default RTC job results are stored in our databases. This means that you will need to query our results endpoint and retrieve content yourself. Custom storage feature allows you to store results in your own cloud storage. The advantage of this feature is that you don't have to make extra requests in order to fetch results – everything goes directly to your storage bucket.
We support Amazon S3 and Google Cloud Storage. If you would like to use a different type of storage, please contact your account manager to discuss the feature delivery timeline.
Amazon S3
To get your job results uploaded to your Amazon S3 bucket, please set up access permissions for our service. To do that, go to https://s3.console.aws.amazon.com/ > S3 > Storage > Bucket Name (if don't have one, create new) > Permissions > Bucket Policy
You can find bucket policy in this JSON or in code sample area on the right. Don't forget to change bucket name under YOUR_BUCKET_NAME
. This policy allows us to write to your bucket, give access to uploaded files to you, and know bucket location.
Google Cloud Storage
To get your job results uploaded to your Google Cloud Storage bucket, please set up special permissions for our service. To do that, please create a custom role with the storage.objects.create
permission and assign it to the Oxylabs service account email [email protected]
.
Usage
To use this feature, please specify two additional parameters in your requests. Learn more 这里.
The upload path looks like this: YOUR_BUCKET_NAME/job_ID.json
. You will find job ID in response body that you receive from us after submitting a request. In this example job ID is 12345678900987654321
.
{ "Version": "2012-10-17", "Id": "Policy1577442634787", "Statement": [ { "Sid": "Stmt1577442633719", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::324311890426:user/oxylabs.s3.uploader" }, "Action": "s3:GetBucketLocation", "Resource": "arn:aws:s3:::YOUR_BUCKET_NAME" }, { "Sid": "Stmt1577442633719", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::324311890426:user/oxylabs.s3.uploader" }, "Action": [ "s3:PutObject", "s3:PutObjectAcl" ], "Resource": "arn:aws:s3:::YOUR_BUCKET_NAME/*" } ] }
Realtime
The data submission is the same as in Push-Pull method, but with Realtime, we will return the content on open connection. You send us a query, the connection remains open, we retrieve the content, and bring it to you. The endpoint that handles that is this:
POST https://realtime.oxylabs.io/v1/queries
The timeout limit for open connections is 100 seconds. Therefore, in rare cases of heavy load, we may not be able to ensure the data gets to you.
You need to post query parameters as data in the JSON body. Please see an example for more details.
curl --user user:pass1 'https://realtime.oxylabs.io/v1/queries' -H "Content-Type: application/json" \ -d '{"source": "universal", "url": "https://stackoverflow.com/questions/tagged/python"}'
Example response body that will be returned on open connection:
{ "results": [ { "content": " CONTENT " "created_at": "2019-10-01 00:00:01", "updated_at": "2019-10-01 00:00:15", "id": null, "page": 1, "url": "https://stackoverflow.com/questions/tagged/python", "job_id": "12345678900987654321", "status_code": 200 } ] }
SuperAPI
If you have ever used regular proxies for data scraping, integrating SuperAPI delivery method will be a breeze. You simply need to use our entry node as proxy, authorize with HTML Crawler API credentials, and ignore certificates. In cURL
it's -k
或 --insecure
. Your data will reach you on open connection.
GET realtime.oxylabs.io:60000
SuperAPI only supports a handful of parameters since it only works with Direct data source where a full URL is provided. These parameters should be sent as headers. This is a list of accepted parameters:
X-OxySERPs-User-Agent-Type |
There is no way to indicate a specific User-Agent, but you can let us know which browser and platform to use. A list of supported User-Agents can be found 这里. |
If you need help setting up SuperAPI, get in touch with us at [email protected].
curl -k \ -x realtime.oxylabs.io:60000 \ -U user:pass1 \ -H "X-OxySERPs-User-Agent-Type: desktop_chrome" \ "https://stackoverflow.com/questions/tagged/python"
Content Type
HTML Crawler API returns raw HTML.
Download Images
It is possible to download images via HTML Crawler API. If you are doing that through SuperAPI, you can simply save the output to image extension. For example:
curl -k -x realtime.oxylabs.io:60000 -U user:pass1 "https://example.com/image.jpg" >> image.jpg
If you are using Push-Pull 或 Realtime methods, you will need to add content_encoding
parameter with a value of base64
. Once you receive the results, you then need to decode encoded data from content
into bytes and save it as an image file. Please find an example in Python on the right.
Data Sources
HTML Crawler API accepts URLs, along with additional parameters, such as User-Agent type, proxy location, and others. See this method, which we call Direct, described below.
HTML Crawler API is able to render JavaScript when scraping. This enables you to get more data from the web page and get screenshots.
If you are unsure about any part of the documentation, drop us a line at [email protected] or contact your account manager.
Direct
universal
source is designed to retrieve the contents of any URL on the internet. 职位
-ing the parameters in JSON format to the following endpoint will submit the specified URL to the extraction queue.
查询参数
参数 | 说明 | Default Value |
source | Data source | universal |
网址 | Direct URL (link) to Universal page | – |
user_agent_type | Device type and browser. The full list can be found 这里。 | desktop |
geo_location | Geo location of proxy used to retrieve the data. The full list of supported locations can be found 这里。 | |
locale | Locale, as expected in Accept-Language header. | |
给予 | Enables JavaScript rendering. Use it when the target requires JavaScript to load content. Only works via Push-Pull (a.k.a. Callback) method. There are two available values for this parameter: html(get raw output) and png (get a Base64-encoded screenshot). | |
content_encoding | Add this parameter if you are downloading images. Learn more 这里。 | base64 |
context: | Base64-encoded POST request body. It is only useful if http_method is set to post. | – |
content | ||
context: | Pass your own cookies. | – |
cookies | ||
context: | Indicate whether you would like the scraper to follow redirects (3xx responses with a destination URL) to get the contents of the URL at the end of the redirect chain. | – |
follow_redirects | ||
context: | Pass your own headers. | – |
页眉 | ||
context: | Set it to post if you would like to make a POST request to your target URL via Universal scraper. | 获取 |
http_method | ||
context: | If you want to use the same proxy with multiple requests, you can do so by using this parameter. Just set your session to any string you like, and we will assign a proxy to this ID and keep it for up to 10 minutes. After that, if you make another request with the same session ID, a new proxy will be assigned to that particular session ID. | – |
session_id | ||
context: | Define a custom HTTP response code (or a few of them), upon which we should consider the scrape successful and return the content to you. May be useful if you want us to return the 503 error page or in some other non-standard cases. | – |
successful_status_codes | ||
callback_url | URL to your callback endpoint. | – |
storage_type | Storage service provider. We support Amazon S3 and Google Cloud Storage. The storage_type parameter values for these storage providers are, correspondingly, s3 and gcs. The full implementation can be found on the Upload to Storage page. This feature only works via Push-Pull (Callback) method. | – |
storage_url | Your storage bucket name. Only works via Push-Pull (Callback) method. | – |
– required parameter |
In this example, the API will retrieve a universal product page in Push-Pull method. All available parameters are included (though not always necessary or compatible within the same request), to give you an idea on how to format your requests:
curl --user user:pass1 \ 'https://data.oxylabs.io/v1/queries' \ -H "Content-Type: application/json" \ -d '{"source":"universal","url":"https://stackoverflow.com/questions/tagged/python","user_agent_type":"mobile","context":[{"key":"headers","value":{"Accept-Language":"en-US","Content-Type":"application/octet-stream","Custom-Header":"custom header content"}},{"key":"cookies","value":[{"key":"NID","value":"1234567890"},{"key":"1P JAR","value":"0987654321"}]},{"key":"follow_redirects","value":true},{"key":"http_method","value":"post"},{"key":"content","value":"YmFzZTY0RW5jb2RlZFBPU1RCb2R5"},{"key":"successful_status_codes","value":[808,909]}]}
Here is the same example in Realtime:
curl --user user:pass1 \ 'https://data.oxylabs.io/v1/queries' \ -H "Content-Type: application/json" \ -d '{"source": "universal", "url": "https://stackoverflow.com/questions/tagged/python", "user_agent_type": "mobile", "context": [{"key": "headers", "value": ["Accept-Language": "en-US", "Content-Type": "application/octet-stream", "Custom-Header": "custom header content"]}, {"key": "cookies", "value": [{"key": "NID", "value": "1234567890"}, {"key": "1P JAR", "value": "0987654321"}, {"key": "follow_redirects", "value": true}, {"key": "http_method", "value": "post"}, {"key": "content", "value": "base64EncodedPOSTBody"}, {"key": "successful_status_codes", "value": [303, 808, 909]}]}]}'
And via SuperAPI:
# A GET request could look something like this: curl -k \ -x http://realtime.oxylabs.io:60000 \ -U user:pass1 \ "https://stackoverflow.com/questions/tagged/python" \ -H "X-OxySERPs-Session-Id: 1234567890abcdef" \ -H "X-OxySERPs-Geo-Location: India" \ -H "Accept-Language: en-US" \ -H "Content-Type: application/octet-stream" \ -H "Custom-Header: custom header content" \ -H "Cookie: NID=1234567890; 1P_JAR=0987654321" \ -H "X-Status-Code: 303, 808, 909" # A POST request would have the same structure but contain a parameter specifying that it is a POST request: curl -X POST \ -k \ -x http://realtime.oxylabs.io:60000 \ -U user:pass1 "https://stackoverflow.com/questions/tagged/python" \ -H "X-OxySERPs-Session-Id: 1234567890abcdef" \ -H "X-OxySERPs-Geo-Location: India" \ -H "Custom-Header: custom header content" \ -H "Cookie: NID=1234567890; 1P_JAR=0987654321" \ -H "X-Status-Code: 303, 808, 909"
Parameter Values
Geo_Location
Full list of supported geo locations can be found in CSV format 这里.
"United Arab Emirates", "Albania", "Armenia", "Angola", "Argentina", "Australia", ... "Uruguay", "Uzbekistan", "Venezuela Bolivarian Republic of", "Viet Nam", "South Africa", "Zimbabwe"
HTTP_Method
Universal Crawler supports two HTTP(S) methods: 获取
(default) and 职位
.
"GET", "POST"
Render
Universal Crawler can render Javascript and return either a rendered HTML document or a PNG screenshot of the web page.
"html", "png"
User_Agent_Type
Download full list of user_agent_type
values in JSON 这里.
[ { "user_agent_type": "desktop", "description": "Random desktop browser User-Agent" }, { "user_agent_type": "desktop_firefox", "description": "Random User-Agent of one of the latest versions of desktop Firefox" }, { "user_agent_type": "desktop_chrome", "description": "Random User-Agent of one of the latest versions of desktop Chrome" }, { "user_agent_type": "desktop_opera", "description": "Random User-Agent of one of the latest versions of desktop Opera" }, { "user_agent_type": "desktop_edge", "description": "Random User-Agent of one of the latest versions of desktop Edge" }, { "user_agent_type": "desktop_safari", "description": "Random User-Agent of one of the latest versions of desktop Safari" }, { "user_agent_type": "mobile", "description": "Random mobile browser User-Agent" }, { "user_agent_type": "mobile_android", "description": "Random User-Agent of one of the latest versions of Android browser" }, { "user_agent_type": "mobile_ios", "description": "Random User-Agent of one of the latest versions of iPhone browser" }, { "user_agent_type": "tablet", "description": "Random tablet browser User-Agent" }, { "user_agent_type": "tablet_android", "description": "Random User-Agent of one of the latest versions of Android tablet" }, { "user_agent_type": "tablet_ios", "description": "Random User-Agent of one of the latest versions of iPad tablet" } ]
Account Status
Usage Statistics
You can find your usage statistics by querying the following endpoint:
GET https://data.oxylabs.io/v1/stats
By default, the API will return all-time usage statistics. Adding ?group_by=month
will return monthly stats, while ?group_by=day
will return daily numbers.
This query will return all-time statistics. You can find your daily and monthly usage by adding either ?group_by=day
或 ?group_by=month
curl --user user:pass1 'https://data.oxylabs.io/v1/stats'
Sample output:
{ "data": { "sources": [ { "realtime_results_count": "90", "results_count": "10", "title": "universal" } ] }, "meta": { "group_by": null } }
Limits
The following endpoint will give your monthly commitment information as well as how much of it has already been used:
GET https://data.oxylabs.io/v1/stats/limits
curl --user user:pass1 'https://data.oxylabs.io/v1/stats/limits'
Sample output:
{ "monthly_requests_commitment": 4500000, "used_requests": 985000 }
Response Codes
Code | Status | 说明 |
---|---|---|
204 |
No Content | You are trying to retrieve a job that has not been completed yet. |
400 |
Multiple error messages | Bad request structure, could be a misspelled parameter or invalid value. The response body will have a more specific error message. |
401 |
‘Authorization header not provided' / ‘Invalid authorization header' / ‘Client not found' | Missing authorization header or incorrect login credentials. |
403 |
Forbidden | Your account does not have access to this resource. |
404 |
Not Found | Job ID you are looking for is no longer available. |
429 |
Too many requests | Exceeded rate limit. Please contact your account manager to increase limits. |
500 |
Unknown Error | Service unavailable. |
524 |
Timeout | Service unavailable. |
612 |
Undefined Internal Error | Something went wrong and we failed the job you submitted. You can try again at no extra cost, as we do not charge you for faulted jobs. If that does not work, please get in touch with us. |
613 |
Faulted After Too Many Retries | We tried scraping the job you submitted, but gave up after reaching our retry limit. You can try again at no extra cost, as we do not charge you for faulted jobs. If that does not work, please get in touch with us. |
References
- https://docs.oxylabs.io/rtc/source/html-crawler-api/index.html
- https://docs.oxylabs.io/rtc/index.html
Disclaimer: This part of the content is mainly from the merchant. If the merchant does not want it to be displayed on my website, please 联系我们 删除您的内容。
最后更新于 5 月 16, 2022