如何使用JQuery/Ajax调用Apify Google Search Scrapper任务? [英] How to call Apify Google Search Scrapper Task Using JQuery/Ajax?

查看:161
本文介绍了如何使用JQuery/Ajax调用Apify Google Search Scrapper任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习通过他们的API呼叫使用apify/google-search-scraper.在此处获得该文档.

I am learning to use apify/google-search-scraper through their API call. The document is given here.

我是新来的,我对他们的文档感到有些困惑.特别是我需要帮助来配置呼叫.

I am bit confused with their documentation as I am new. Especially I need the help to configure the call. It

$.ajax({
   url : '',  
   method : "POST",
   contentType: "application/json; charset=utf-8",

   data : {   

   },
   success:function(response) {
     console.log(response.data); 
   } 

 });

URL :我应该在这里写些什么?

url: what should I write here?

数据:我应该在这里传递参数吗?

data : Should I pass parameters here?

谢谢.

推荐答案

您需要使用

You need to use run task API endpoint to run it. You can use synchronous run same as asynchronous.

如果要使用AJAX运行端点,则可以使用:

If you want to run the endpoint using AJAX you can use:

$.ajax({
   url : 'https://api.apify.com/v2/actor-tasks/<your task name>/runs?token=<your api token>',  
   method : 'POST',
   contentType: 'application/json; charset=utf-8',
   success:function(response) {
     console.log(response.data); // Actor run object
   } 

 });

如果您还需要从任务运行中获取数据,则需要等待其完成.然后使用获取数据集项目API从默认数据集中获取数据端点.好消息是,您可以在调用run时使用waitForFinish参数,并等待其完成.

If you need to get data from task run as well you need to wait until it finishes. Then get data from default dataset using get dataset items API endpoint. The good thing is that you can use waitForFinish param in calling run and it waits for it finishes.

const getItemsFromDataset = (datasetId) => {
    $.ajax({
       url : `https://api.apify.com/v2/datasets/${datasetId}/items?format=json`,  
       method : 'GET',
       contentType: 'application/json; charset=utf-8',
       success:function(response) {
         console.log(response); // Items from dataset
       } 

     });
}

$.ajax({
   url : 'https://api.apify.com/v2/actor-tasks/<your task name>/runs?token=<your api token>&waitForFinish=120',  
   method : 'POST',
   dataType: 'json',
   data : JSON.stringify ({
      "queries" : "query you want to"
   }),
   success:function(response) {
     console.log(response.data); // Actor run object
     getItemsFromDataset(response.data.defaultDatasetId) 
   } 

 });

您需要完成示例中的错误处理.

You need to finish error handling in examples.

添加了查询参数,以覆盖您要抓取的查询.

Added queries param to override query you want to scrape.

这篇关于如何使用JQuery/Ajax调用Apify Google Search Scrapper任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆