简体   繁体   English

Zapier上的Javascript HTTP获取请求

[英]Javascript HTTP fetch request on Zapier

I am using a Javascript step on Zapier to scrape html from a URL with the following: 我在Zapier上使用Javascript步骤,使用以下内容从网址中抓取html:

fetch('http://printatestpage.com') //example url
  .then(function(res) {
    return res.text();
  })
  .then(function(body) {
    var output = {id: 1234, rawHTML: body};
    callback(null, output);
  })
  .catch(callback);

This works great, however I do not need the full HTML body response. 这很好用,但是我不需要完整的HTML正文响应。

Is it possible to only output a specific div? 是否只能输出特定的div? For instance in the above code say I only wanted the response to output html from class PrintButtons ? 例如在上面的代码中说我只希望响应从类PrintButtons输出html?

Unless that host provides some rest API or another service that allows you to directly ask for a specific div, you will have to get the full html and then get the div by id or class name in plain Javascript (or in JQuery or another library). 除非该主机提供了一些其他的API或其他服务,允许您直接请求特定的div,否则您将必须获取完整的html,然后以纯Javascript(或JQuery或其他库)中的id或类名获取div。 。

In case you have the HTML as string, you might need a DOM Parser 如果您将HTML作为字符串,则可能需要DOM解析器

You can parse the rawHTML and use jQuery-like DOM manipulation using cheerio 您可以解析rawHTML并使用cheerio使用类似jQuery的DOM操作

https://www.npmjs.com/package/cheerio https://www.npmjs.com/package/cheerio

const cheerio  = require('cheerio')
const $ = cheerio.load(rawHTML)
const elementHTMLString = $('.PrintButtons').html()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM