For the complete documentation index, see llms.txt. This page is also available as Markdown.

Realtime

Oxylabs 网页爬虫API 的 Realtime 集成。从任务提交开始保持 HTTPS 连接打开,直至返回结果或错误,并使用 JSON 格式的负载。

Realtime 是一种同步集成方法。它 需要保持连接打开 直到作业成功完成或返回错误。它是实现最快的方法;对于大量数据,请使用 Push-Pull 替代。

作业提交

端点

用于作业提交的 Realtime API 端点是:

POST https://realtime.oxylabs.io/v1/queries

输入

请在 JSON 载荷中提供作业参数,如下方示例所示。Python 和 PHP 示例包含注释以便说明。

curl --user \"USERNAME:PASSWORD\" \\
'https://realtime.oxylabs.io/v1/queries' \\
-H \"Content-Type: application/json\" \\
-d '{"source": "universal", "url": "https://example.com", "geo_location": "United States"}'
import requests
from pprint import pprint


# Structure payload.
payload = {
    "source": "universal", # Source you choose e.g. "universal"
    "url": "https://example.com", # Check the docs of the specific source you're using to see if you should use "url" or "query"
    "geo_location": "United States", # Some sources accept post codes and/or coordinates
    #"render" : "html", # Uncomment if you want to render JavaScript on the page
    #"render" : "png", # Uncomment if you want to take a screenshot of a scraped web page
    #"parse" : True, # Check what sources support parsed data
}

# Get response.
response = requests.request(
    'POST',
    'https://realtime.oxylabs.io/v1/queries',
    auth=('YOUR_USERNAME', 'YOUR_PASSWORD'), #Your credentials go here
    json=payload,
)

不再返回带有作业状态和结果 url 的响应,而是返回
包含结果的 JSON 响应。
pprint(response.json())

输出

Realtime API 在输出中支持以下结果类型:

  • HTML:从目标网页抓取的原始 HTML 内容;

  • JSON:从 HTML 内容解析出的结构化数据,以 JSON 格式呈现;

  • PNG:以 PNG 格式编码的已渲染页面 Base64 截图。

  • XHR: XHR 请求 在加载页面时发出的。

  • Markdown: Markdown 网页的。

此表说明了根据 API 请求载荷中包含的参数,默认结果类型:

渲染参数
解析参数
默认输出

-

-

html

html

-

html

png

-

png

-

true

json

html

true

json

png

true

png

默认情况下,Realtime 仅返回 默认输出 作为你的载荷。若要改为接收一个或多个可用输出,请添加 type 查询参数添加到端点中,如下所示。每个请求的 type 都会作为单独条目返回到 results 数组。这就是 多结果类型 功能,并且在 Realtime 和 Push-Pull.

某些来源有一个 专用解析器;请使用 parse: true 用于这些来源。对于任何其他目标,请使用 自定义解析器。 每个已解析结果都带有一个 parse_status_code;请参见 响应代码 以了解各值的含义。

输出示例:

响应还包含 _request, _response,以及 session_info 对象,出于简洁起见此处省略。使用 parse 时, parser_typeparser_preset 也会一并返回。

最后更新于

这有帮助吗?