使用jsoup提交文本搜索 [英] submitting a text search using jsoup

查看:164
本文介绍了使用jsoup提交文本搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段html代码,代表一个网站的一部分,该网站应该是大学教师目录的搜索部件。

 < div id =right_columnclass =content_main> 
< div class =searchbox>
< form method =POSTaction =/ faculty / directory_search /id =searchform>
< h4>搜寻目录< / h4>
< input type =textname =searchtermsvalue =/>
< select name =categoryclass =dropdown> < option value =allselected =selected> All Categories< / option> < option value =Faculty> Faculty< / option> < option value =Staff> Staff< / option> < option value =访客>访客< / option> < option value =全职>全职< / option> < option value =Visiting>访问< / option> < option value =特别预约>特别预约< / option> <选项值=生物科学>生物科学< / option> < /选择>
< input type =hiddenname =sortvalue =asc/>
< input type =submitclass =submitvalue =Search directory/>
< a class =buttonhref =/ faculty / index / descid =sortbutton>按字母排序< / a>
< / form>
< script type =text / javascript>
$('#searchform')。ready(function(){
$('#sortbutton')。click(function(){
$('input [name =sort ]')。val('desc');
$('#searchform')。submit();
return false;
});
});
< / script>
< / div>

我试图输入名称john并使用jsoup使用以下java代码提交搜索(用于android,但与普通Java应用程序的Java代码基本相同)



$ p $ Document doc = Jsoup.connect( http://www.qatar.cmu.edu/directory/\").data(\"searchterms,john)。post();

然而,我一直在获取相同的页面,只是 http://www.qatar.cmu.edu/directory/ ,不提交任何搜索。我注意到,在HTML代码中有提交输入类型。我想知道我是否必须提交搜索。如果是这样,怎么办?

解决方案

我相信你正在执行POST请求到包含表单的页面,而不是形式端点。这应该工作:

pre $ doc $ doc = Jsoup.connect data(search,john)。data(sort,asc)。data(category,all)。post();

它直接向表单端点发送POST请求。


I have a piece of html code which represents a part of a website that is supposed to be the search widget for a directory of a faculty in a university

 <div id="right_column" class="content_main"> 
    <div class="searchbox"> 
     <form method="POST" action="/faculty/directory_search/" id="searchform"> 
      <h4>Search the Directory</h4> 
      <input type="text" name="searchterms" value="" /> 
      <select name="category" class="dropdown"> <option value="all" selected="selected">All Categories</option> <option value="Faculty">Faculty</option> <option value="Staff">Staff</option> <option value="Visitors">Visitors</option> <option value="Full time">Full time</option> <option value="Visiting">Visiting</option> <option value="Special Appointment">Special Appointment</option> <option value="Biological Sciences">Biological Sciences</option> </select> 
      <input type="hidden" name="sort" value="asc" /> 
      <input type="submit" class="submit" value="Search directory" /> 
      <a class="button" href="/faculty/index/desc" id="sortbutton">Sort Alphabetically</a> 
     </form> 
     <script type="text/javascript">
                $('#searchform').ready(function(){
                    $('#sortbutton').click(function(){
                        $('input[name="sort"]').val('desc');
                        $('#searchform').submit();
                        return false;
                    });
                });
            </script> 
    </div> 

I am trying to input the name "john" and submit the search using jsoup using the following java code (intended for android but it's overall the same java code as for a regular java app)

Document doc = Jsoup.connect("http://www.qatar.cmu.edu/directory/").data("searchterms", "john").post();

However, I keep getting the same page as just "http://www.qatar.cmu.edu/directory/" with no search submitted. I noticed that in the html code there is the submit input type. I'm wondering if I had to submit the search. If so, how can it be done?

解决方案

I believe you are performing a POST request to the page containing the form, not the forms endpoint. This should work:

Document doc = Jsoup.connect("http://www.qatar.cmu.edu/faculty/directory_search/").data("searchterms", "john").data("sort", "asc").data("category", "all").post();

It makes the POST request directly to the forms endpoint.

这篇关于使用jsoup提交文本搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆