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

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

问题描述

我正在通过他们的 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?

提前致谢.

推荐答案

您需要使用 运行任务 API 端点 来运行它.您可以使用与异步相同的同步运行.

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
   } 

 });

如果您还需要从任务运行中获取数据,则需要等到它完成.然后使用 get dataset items 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 Scraper Task?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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