如何使用节点抓取器单击页面中的 'submit' 按钮,然后检索它转到的页面

How to click a 'submit' button in page with node scraper and then retrieve the page it goes to

我正在 node 中构建一个 scraper,但我被难住了。我转到这个地址:https://ariisp1.oklahomacounty.org/AssessorWP5/DefaultSearch.asp,我想模拟将地址放入第二个文本框并单击随后的 'Submit' 按钮。我可以成功找到文本框和下面的按钮,但我不知道如何模拟 'click' 函数并检索它去的 url 。没有与按钮关联的 href,因为它转到的 url 由输入的地址决定。有什么想法吗?

var request = require('request');
var cheerio = require('cheerio');


request.post('https://ariisp1.oklahomacounty.org/AssessorWP5/DefaultSearch.asp', /*{
  form: {
    FormattedLocation: '2333 Nw 32 St'
    //btnSubmit: 'Submit'
  }
}, */
function (err, res, body) {
    let $ = cheerio.load(body);


    $("input[name=FormattedLocation]").text('2331 nw 32 st');
    var x = $("input[name=FormattedLocation]").text();
    var y = $("input[name=FormattedLocation]").next().attr('type');

    console.log(y);//successfully gets the 'Submit' button

    //code to click button and get the page it goes to goes here
})

你不需要用cheerio点击按钮,你需要做的是发出POST请求然后解析结果。目前还不完全清楚您要对结果做什么,而您的示例代码基本上已经这样做了。困难的部分是您得到的表单响应完全是 HTML 的一团糟,并且您没有告诉我们您试图从中提取什么。

request.post('https://ariisp1.oklahomacounty.org/AssessorWP5/AddressSearch.asp', { form: { FormattedLocation: '2333 Nw 32 St' }}, (err, res, body) => console.log(body)); 

如果你 运行 那样,你会看到你的请求发生并得到 HTML 回来,就好像你点击了相关的提交按钮一样。从那里开始,你用 cheerio 做什么取决于你想从响应中提取什么 HTML。