-2

我正在尝试使用 Postman 从请求中获取一个返回 HTML 响应的值。我在脚本部分使用 Cheerio。

响应如下所示:

    <table class="etlsessions-table" cellpadding="0" cellspacing="0">
        <thead>
            <tr>
                <th class="someclass1">
                    <div>
                        <span>info1</span>
                    </div>
                </th>
                <th class="someclass2">
                    <div>
                        <span>info2</span>
                    </div>
                </th>
                <th class="someclass3">
                    <div>
                        <span>info3</span>
                    </div>
                </th>
                <th class="someclass2">
                    <div>
                        <span>info4</span>
                    </div>
                </th>
            </tr>
        </thead>
        <tbody>
            <tr class="someclass5">
                <td class="someclass">
                    <nobr>info5</nobr>
                </td>
                <td class="someclass6">
                    <nobr>info6</nobr>
                </td>
                <td class="someclass3">info7</td>
                <td class="someclass7">
                    <a href="http://www.google.com">someurl1</a>
                </td>
            </tr>
       </tbody>
    </table>

我如何从课堂上获得info6价值?someclass6

4

2 回答 2

-1

Postman 是一款允许您调用 API 端点的软件,因此基本上您的程序将使用 node.js 编写,并且您将使用 postman 调用端点。

在这种情况下并使用cheerio,代码将如下所示:

function getResponse() {

return fetch(`${YOUR API END POINT URL}`)
.then(response => response.text())
.then(body => {
  const $ = cheerio.load(body);
  const $info6= $('.someclass6 td );

  const info6= $info6.first().contents().filter(function() {
    return this.type === 'text';
  }).text().trim();
  const response= {
    info6,
  };

  return response;
});
}

祝你好运!

于 2021-05-24T14:39:04.887 回答
-1

由于 Cheerio 内置在 Postman 沙箱环境中,您可以使用它来获取元素的值。

我不确定您的完整用例,但您可以在Tests脚本中添加类似这样的基本内容并将值打印到控制台:

const $ = cheerio.load(pm.response.text()),
    elementValue = $('.someclass6 nobr').text();

console.log(elementValue)
于 2021-05-24T15:49:35.340 回答