【问题标题】:How to change an Apify actor parameter via API如何通过 API 更改 Apify 演员参数
【发布时间】:2020-04-03 20:40:46
【问题描述】:

我想调用 Apify 演员并通过调用 Apify API 来指定参数值。

演员是Google Search Results Scraper located here

Here is where the docs say 使用queries 作为 API 调用负载中的对象属性名称。

下表显示了由其输入模式定义的参与者 INPUT 字段的规范。当使用 API 运行 actor 时,可以 [...] 在 JSON 对象中提供这些字段。在文档中阅读更多内容。

...

搜索查询或网址

Google 搜索查询(例如纽约市的食物)和/或完整网址(例如https://www.google.com/search?q=food+NYC)。

每行输入一项。

可选
类型:字符串

JSON 示例
"queries": "Hotels in NYC
  Restaurants in NYC
  https://www.google.com/search?q=restaurants+in+NYC"

运行我的 Google Apps 脚本代码后,我希望看到 searchQueries.term 参数发生如下变化。

Apify——我期望看到的
"searchQuery": {
  "term": "Banks in Phoenix", // what I am trying to change to by API call
  // [...]
},

但我实际得到的是与上次手动运行actor时相同的参数值。如下。

Apify——我实际看到的
"searchQuery": {
  "term": "CPA firms in Newark", // remaining from last time I ran the actor manually
  // [...]
},

这是我从 Google Apps 脚本运行的代码。

代码.gs
const runSearch = () => {
  const apiEndpoint= `https://api.apify.com/v2/actor-tasks/<MY-TASK-NAME>/run-sync?token=<MY-TOKEN>`
  const formData = {
    method: 'post',
    queries: 'Banks in Phoenix',
  };
  const options = {
    body: formData,
    headers: {
      'Content-Type': 'application/json',
    },
  };
  UrlFetchApp.fetch(apiEndpoint, options,);
}

我做错了什么?

【问题讨论】:

  • 查询看起来不错。我只是不知道 UrlFetchApp 的详细信息。通常,您需要发送正文:JSON.stringify(formData) 和方法:'POST'
  • 您没有收到任何错误消息吗?另外,您能否尝试遵循@LukášKřivka 建议在体内使用JSON.stringify(formData) 并让我知道这是否有效?谢谢!

标签: google-apps-script web-scraping apify


【解决方案1】:

您在request 对象中缺少payload 属性。

改变:
queries: 'Banks in Phoenix',
到:
payload: {
  queries: 'Banks in Phoenix',
}
代码.gs
const runSearch = () => {
  const apiEndpoint= `https://api.apify.com/v2/actor-tasks/<MY-TASK-NAME>/run-sync?token=<MY-TOKEN>`
  const formData = {
    method: 'post',
    payload: {
      queries: 'Banks in Phoenix',
    },
  };
  const options = {
    body: formData,
    headers: {
      'Content-Type': 'application/json',
    },
  };
  UrlFetchApp.fetch(apiEndpoint, options,);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多