【问题标题】:Jsoup - applying a search of a <form> <input> tagJsoup - 应用 <form> <input> 标签的搜索
【发布时间】:2016-06-28 14:48:36
【问题描述】:

我得到了这个网站http://findmusicbylyrics.com/,它使用&lt;form&gt;&lt;input&gt; 标签来输入和提交搜索。

相关 HTML:

<form action="/search.php" id="cse-search-box">
    <div>
        <input type="hidden" name="cx" value="partner-pub-1936238606905173:1893984547" />
        <input type="hidden" name="cof" value="FORID:10" />
        <input type="hidden" name="ie" value="UTF-8" />
        <input type="search" name="q" id="main_search_box" autofocus="autofocus" />
        <input type="submit" name="sa" value="Search" id="main_search_button" />
    </div>
</form>

我想用Jsoup进入我的String,应用搜索并得到HTML的搜索页面。

我的代码:

try {
    HtmlPage htmlPage = new HtmlPage(Jsoup.connect("http://findmusicbylyrics.com/").get()); // Connect to the HTML page
    Element formElement = htmlPage.getElement("form#cse-search-box"); // <form action="/search.php" id="cse-search-box"> tag
    Element searchBoxElement = formElement.select("input#main_search_box").first(); // <input type="search" name="q" id="main_search_box" autofocus="autofocus" /> tag
    Element searchBoxElement2 = searchBoxElement.attr("input", "Love");
} catch (IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

如果我离开这里,我如何离开这里,因为我不知道Jsoup 是否可以处理这种事情。

【问题讨论】:

    标签: java html forms input jsoup


    【解决方案1】:

    你很幸运,该网站正在使用GET 将信息传递到搜索页面。

    如果我是你,我会做的是使用搜索页面的直接 URL 并连接我的关键字。不要忘记将单词之间的空格替换为+

    String keyWord = "hello, its me".replace(" ", "+");
    HtmlPage(Jsoup.connect("http://findmusicbylyrics.com/search.php?cx=partner-pub-1936238606905173%3A1893984547&cof=FORID%3A10&ie=UTF-8&q=" + keyWord + "&sa=Search+Lyrics").get());
    

    【讨论】:

    • 好吧,我改了。但是,当我在搜索框中找到带有关键字的HTML 页面后,我该怎么办?点击搜索按钮发生?谢谢。
    • @God 实际上不需要点击。这将立即使用其中的结果向页面收费。
    • 嗯,我明白了。它只是在搜索提交后将HTML 文档返回给我。很聪明。
    • @God 就是这样 :) 你很幸运,他们通过 URL 而不是通过 POST 发送搜索查询 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 2012-05-18
    相关资源
    最近更新 更多