跳转到主要内容

IDE 交互代码

以下是您可以在 IDE 中使用的所有交互代码: input - 全局对象,可在交互代码中使用,由触发器 inputnext_stage() 提供
navigate(input.url);

navigate - 导航浏览器会话到指定 URL
  • url:要导航到的 URL
navigate([url]);
navigate(input.url);
navigate('https://example.com');
navigate 选项
// 等待 DOM 内容加载完成事件触发
navigate([url], {wait_until: 'domcontentloaded'});

// 添加 referer
navigate([url], {referer: [url]});

// 等待毫秒数,默认 30000 ms
navigate([url], {timeout: 45000});

// 添加请求头
navigate([url], {header : 'accept: text/html'});

// 指定浏览器宽高
navigate([url], {fingerprint: {screen: {width: 400, height: 400}}});

parse - 解析页面数据
let page_data = parse();

collect - 向爬虫创建的数据集添加一条数据
  • data_line:要收集的字段对象
  • validate_fn:可选函数,用于验证数据行有效性
collect([data_line], [validate_fn]);
collect({ title: page_data.title, price: page_data.price });
collect({ price: data.price });

collect(line, l => !l && throw new Error('Empty line'));

next_stage - 使用指定输入运行爬虫下一阶段
  • input:传递给下一浏览器会话的输入对象
next_stage({url: 'http://example.com', page: 1});

rerun_stage - 使用新的 input 重新运行本阶段爬虫
  • input:传递给下一浏览器会话的输入对象
rerun_stage({url: 'http://example.com/other-page'});

run_stage - 使用新浏览器会话运行特定阶段
  • input:传递给下一浏览器会话的输入对象
  • stage:要运行的阶段(1 为第一阶段)
run_stage(2, {url: 'http://example.com', page: 1});

country - 配置爬虫从特定国家运行
  • code:ISO 两位国家代码
country(<code>);

wait - 等待元素出现在页面上
  • selector:元素选择器
  • opt:等待选项(参见示例)
wait(<selector>);
wait('#welcome-splash');
wait('.search-results .product');
wait('[href^="/product"]');
wait(<selector>, {timeout: 5000});
wait(<selector>, {hidden: true});

wait_for_text - 等待页面上的元素包含指定文本
  • selector:元素选择器
  • text:要等待的文本
wait_for_text(<selector>, <text>);
wait_for_text('.location', 'New York');

click - 点击元素(点击前会等待元素出现)
  • selector:元素选择器
click(<selector>);
click('#show-more');

type - 在输入框中输入文本(出现后输入)
  • selector:元素选择器
  • text:要输入的文本
type(<selector>, <text>);
type('#location', 'New York');
type(<selector>, ['Enter']);
type(<selector>, ['Backspace']);

select - 从下拉框中选择值
  • selector:元素选择器
select(<select>, <value>);
select('#country', 'Canada');

URL - NodeJS 标准模块 “url” 的 URL 类
  • url:URL 字符串
let u = new URL('https://example.com');

location - 当前位置信息对象,可用字段:href
navigate('https://example.com');
location.href;

tag_response - 保存浏览器请求的响应数据
  • name:标记字段名称
  • pattern:匹配 URL 的模式
tag_response(<field>, <pattern>);
tag_response('teams', /\/api\/teams/);
navigate('https://example.com/sports');

let teams = parse().teams;
for (let team of teams) collect(team);

response_header - 返回最后页面加载的响应头
let headers = response_headers(); 
console.log('content-type', headers['content-type']);

console - 输出交互代码日志
console.log(1, 'luminati', [1, 2], {key: value});

load_more - 滚动到底部触发加载更多项目,适用于懒加载或无限滚动页面
  • selector:元素选择器
load_more(<selector>);
load_more('.search-results');

scroll_to - 滚动页面使元素可见
scroll_to(<selector>);
scroll_to('.author-profile');

$ - jQuery 风格的辅助函数
  • selector:元素选择器
$(<selector>);
wait($('.store-card'));

IDE 解析器代码

以下是解析器可用的所有代码: input - 全局变量,可在解析器代码中使用
let url = input.url;

$ - cheerio 实例
更多信息请参阅 cheerio 官方网站
$('#example').text()
$('$example').attr('href')

location - 全局变量,可在解析器代码中使用,包含当前位置信息
let current_url = location.href;