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_URL 和 OXYLABS_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('测试模式生成...');
const schema = await sdk.aiScraper.generateSchema({
user_prompt: '提取页面标题'
});
console.log('Schema:', schema);
} catch (error) {
console.error('模式生成错误:', error.message);
}
}
testGenerateSchema();基本用法
import {
OxylabsAIStudioSDK,
OutputFormat
} from 'oxylabs-ai-studio';
const sdk = new OxylabsAIStudioSDK({
apiKey: 'your_api_key_here',
timeout: 120000,
retryAttempts: 3,
});
async function testScrapeOutputJson() {
try {
console.log('测试同步爬取并以 JSON 输出...');
const options = {
url: 'https://www.freelancer.com',
user_prompt: '提取所有链接',
output_format: OutputFormat.JSON,
geo_location: "US",
schema: {
type: 'object',
properties: {
links: { type: 'array', items: { type: 'string' } }
}
}
};
const results = await sdk.aiScraper.scrape(options);
console.log('同步爬取结果:', results);
} catch (error) {
console.error('同步爬取错误:', error.message);
}
}
testScrapeOutputJson();输入参数
url(字符串):要处理的目标 URL。user_prompt(字符串):关于要提取哪些数据的说明。用于自动生成openapi_schema在使用scrapeWithAutoSchema方法时。output_format(字符串):期望的输出格式。可以是markdown或json。默认是markdown.render_html(boolean):指定在提取前是否渲染页面上的 JavaScript。默认是false.openapi_schema(Record<string, any>):一个定义输出数据结构的 JSON Schema 对象。当output_format设置为时,这是必需的json.geo_location(字符串):指定应模拟请求的地理位置(ISO2 格式)。
AI-Crawler
基本用法
import {
OxylabsAIStudioSDK,
OutputFormat
} from 'oxylabs-ai-studio';
const sdk = new OxylabsAIStudioSDK({
apiKey: 'your_api_key_here',
timeout: 120000,
retryAttempts: 3,
});
async function testCrawlOutputJson() {
try {
console.log('测试爬取并以 JSON 输出...');
const options = {
url: 'https://www.freelancer.com',
output_format: OutputFormat.JSON,
user_prompt: '获取招聘广告页面',
return_sources_limit: 3,
geo_location: "DE",
schema: {
type: "object",
properties: {
jobAd: {
type: "object",
properties: {
position_title: {
type: "string"
},
salary: {
type: "string"
}
}
}
}
}
};
const results = await sdk.aiCrawler.crawl(options);
console.log('爬取结果:', JSON.stringify(results, null, 2));
} catch (error) {
console.error('爬取错误:', error.message);
}
}
testCrawlOutputJson();输入参数
url(字符串):爬取的起始 URL。crawl_prompt(字符串):定义要查找和爬取的页面类型的说明。parse_prompt(字符串):关于从爬取到的页面中提取哪些数据的说明。用于自动生成openapi_schema在使用crawlWithAutoSchema方法时。output_format(字符串):期望的输出格式。可以是markdown或json。默认是markdown.max_pages(整数):要返回的最大页面或来源数量。默认是25.render_html(boolean):指定在提取前是否渲染页面上的 JavaScript。默认是false.openapi_schema(Record<string, any>):一个定义输出数据结构的 JSON Schema 对象。当output_format设置为时,这是必需的json.geo_location(字符串):指定应模拟请求的地理位置(ISO2 格式)。
Browser-Agent
基本用法
import {
OxylabsAIStudioSDK,
OutputFormat
} from 'oxylabs-ai-studio';
const sdk = new OxylabsAIStudioSDK({
apiKey: 'your_api_key_here',
timeout: 120000,
retryAttempts: 3,
});
async function testBrowseOutputJson() {
try {
console.log('测试同步浏览并以 JSON 输出...');
const options = {
url: 'https://www.freelancer.com',
output_format: OutputFormat.JSON,
user_prompt: '导航到你能找到的第一个招聘广告。',
geo_location: "US",
schema: {
type: 'object',
properties: {
job_title: { type: 'string' }
}
}
};
const results = await sdk.browserAgent.browse(options);
console.log('同步浏览结果:', JSON.stringify(results, null, 2));
} catch (error) {
console.error('同步浏览错误:', error.message);
}
}
testBrowseOutputJson();输入参数
url(字符串):浏览器代理开始的目标 URL。browse_prompt(字符串):定义浏览器代理应执行的操作的说明。parse_prompt(字符串):执行浏览器操作后要提取哪些数据的说明。用于自动生成openapi_schema在使用browseWithAutoSchema方法时。output_format(字符串):期望的输出格式。可以是markdown,html,json,或screenshot。默认是markdown.render_html(boolean):指定是否渲染页面上的 JavaScript。尽管这是一个浏览器代理,该标志可能会影响某些行为。默认是false.openapi_schema(Record<string, any>):一个定义输出数据结构的 JSON Schema 对象。当output_format设置为时,这是必需的json.geo_location(字符串):指定应模拟请求的地理位置(ISO2 格式)。
AI-Search
基本用法
import {
OxylabsAIStudioSDK,
} from 'oxylabs-ai-studio';
const sdk = new OxylabsAIStudioSDK({
apiKey: 'your_api_key_here',
timeout: 120000,
retryAttempts: 3,
});
async function testSearch() {
try {
console.log('测试搜索...');
const options = {
query: '伦敦天气',
limit: 3,
return_content: true,
render_javascript: false,
geo_location: "IT",
};
const results = await sdk.aiSearch.search(options);
console.log('搜索结果:', JSON.stringify(results, null, 2));
} catch (error) {
console.error('搜索错误:', error.message);
}
}
testSearch();输入参数
query(字符串):搜索查询。limit(整数):返回的最大搜索结果数。最大值:50。render_javascript(boolean):是否在页面上渲染 JavaScript。默认是false.return_content(boolean):是否返回每个搜索结果的 markdown 内容。默认是true.geo_location(字符串):指定应模拟请求的地理位置(ISO2 格式)。
AI-Map
基本用法
import {
OxylabsAIStudioSDK
} from 'oxylabs-ai-studio';
const sdk = new OxylabsAIStudioSDK({
apiKey: 'your_api_key_here',
timeout: 120000,
retryAttempts: 3,
});
async function testMap() {
try {
console.log('测试映射...');
const options = {
url: 'https://www.freelancer.com/jobs',
user_prompt: '提取技术类招聘广告',
return_sources_limit: 10,
geo_location: 'US',
render_javascript: false
};
const results = await sdk.aiMap.map(options);
console.log('映射结果:', JSON.stringify(results, null, 2));
} catch (error) {
console.error('映射错误:', error.message);
}
}
testMap();输入参数
url(字符串):要映射并提取数据的目标 URL。user_prompt(字符串):关于要从映射的页面中提取哪些数据的说明。return_sources_limit(整数):映射过程要返回的最大来源/页面数量。geo_location(字符串):用于映射请求的地理位置(例如,'US','UK')。render_javascript(boolean):指定在映射前是否渲染页面上的 JavaScript。默认是false.
使用示例
您可以在此处找到每个应用程序的更多示例:
最后更新于
这有帮助吗?

