代码之家  ›  专栏  ›  技术社区  ›  Rainhider

如何使用node scraper单击页面中的“提交”按钮,然后检索它所指向的页面

  •  0
  • Rainhider  · 技术社区  · 6 年前

    我正在建一个铲运机的节点,但我已经被难住了。我去这个地址: https://ariisp1.oklahomacounty.org/AssessorWP5/DefaultSearch.asp 我想模拟在第二个文本框中输入一个地址,然后点击随后的“提交”按钮。我可以成功地找到文本框和下面的按钮,但是我不知道如何模拟“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
    })
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Paul    6 年前

    你不需要用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返回,就像您单击了有问题的submit按钮一样。从这里开始,你对cheerio的处理取决于你想从响应html中提取什么。