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

JavaScript SDK

了解如何使用 AI Studio JavaScript SDK。

我们提供一个 JavaScript SDK,用于无缝对接 Oxylabs AI Studio API 服务交互,包括 AI-Scraper、AI-Crawler、AI-Browser-Agent,以及其他数据提取工具。

安装

安装 SDK:

npm install oxylabs-ai-studio

或者添加 OXYLABS_AI_STUDIO_API_URLOXYLABS_AI_STUDIO_API_KEY 值到 .env 文件中,或者作为环境变量:

export OXYLABS_AI_STUDIO_API_KEY=your_api_key_here

用法

AI-Scraper

import { 
  OxylabsAIStudioSDK
} from 'oxylabs-ai-studio';

const sdk = new OxylabsAIStudioSDK({
  apiKey: 'your_api_key_here',
  timeout: 120000,
  retryAttempts: 3,
});

async function testGenerateSchema() {
  try {
    console.log('Testing schema generation...');
    const schema = await sdk.aiScraper.generateSchema({
      user_prompt: 'Extract the title of the page'
    });
    console.log('Schema:', schema);
  } catch (error) {
    console.error('Schema generation error:', error.message);
  }
}

testGenerateSchema();

基本用法

输入参数

  • url (字符串): 目标 URL。

  • user_prompt (字符串): 要提取哪些数据的说明。这会用于自动生成 openapi_schema 在使用 scrapeWithAutoSchema 方法时。

  • output_format (字符串): 输出所需格式。可以是 MarkdownJSON。默认为 Markdown.

  • render_html (布尔值): 指定在提取前是否在页面上渲染 JavaScript。默认为 false.

  • openapi_schema (Record<string, any>): 定义输出数据结构的 JSON Schema 对象。当 output_format 设置为 JSON.

  • geo_location (字符串): 指定请求应模拟的地理位置(ISO2 格式)。

AI-Crawler

基本用法

输入参数

  • url (字符串): 抓取的起始 URL。

  • crawl_prompt (字符串): 定义要查找并抓取的页面类型的说明。

  • parse_prompt (字符串): 关于从抓取到的页面中提取哪些数据的说明。这会用于自动生成 openapi_schema 在使用 crawlWithAutoSchema 方法时。

  • output_format (字符串): 输出所需格式。可以是 MarkdownJSON。默认为 Markdown.

  • max_pages (整数): 可返回的最大页面或来源数量。默认为 25.

  • render_html (布尔值): 指定在提取前是否在页面上渲染 JavaScript。默认为 false.

  • openapi_schema (Record<string, any>): 定义输出数据结构的 JSON Schema 对象。当 output_format 设置为 JSON.

  • geo_location (字符串): 指定请求应模拟的地理位置(ISO2 格式)。

Browser-Agent

基本用法

输入参数

  • url (字符串): Browser Agent 的起始目标 URL。

  • browse_prompt (字符串): 定义 Browser Agent 应执行的操作的说明。

  • parse_prompt (字符串): 在执行浏览器操作后要提取哪些数据的说明。这会用于自动生成 openapi_schema 在使用 browseWithAutoSchema 方法时。

  • output_format (字符串): 输出所需格式。可以是 Markdown, html, JSON,或 screenshot。默认为 Markdown.

  • render_html (布尔值): 指定是否在页面上渲染 JavaScript。虽然这是一个 Browser Agent,但此标志可能会影响某些行为。默认为 false.

  • openapi_schema (Record<string, any>): 定义输出数据结构的 JSON Schema 对象。当 output_format 设置为 JSON.

  • geo_location (字符串): 指定请求应模拟的地理位置(ISO2 格式)。

基本用法

输入参数

  • query (字符串): 搜索查询。

  • limit (整数): 要返回的搜索结果最大数量。最大值:50。

  • render_javascript (布尔值): 是否在页面上渲染 JavaScript。默认为 false.

  • return_content (布尔值): 是否返回每个搜索结果的 Markdown 内容。默认为 true.

  • geo_location (字符串): 指定请求应模拟的地理位置(ISO2 格式)。

AI-Map

基本用法

输入参数

  • url (字符串): 要映射并提取数据的目标 URL。

  • user_prompt (字符串): 关于从映射页面中提取哪些数据的说明。

  • return_sources_limit (整数): 映射过程中可返回的最大来源/页面数量。

  • geo_location (字符串): 映射请求要使用的地理位置(例如 'US'、'UK')。

  • render_javascript (布尔值): 指定在映射前是否在页面上渲染 JavaScript。默认为 false.

使用示例

你可以在这里找到每个应用的更多示例:

最后更新于

这有帮助吗?