如何在提交搜索表单后解析HTML,以获取来自数据库的数据

huangapple go评论75阅读模式
英文:

How to Parse Html after submitting search form that gives data from data base

问题

Connection.Response redirectResponse = Jsoup.connect(internalConstant.getSearchResultURL())
        .userAgent(USER_AGENT)
        .sslSocketFactory(utilService.socketFactory())
        .cookies(coky)
        .method(Method.GET)
        .execute();

String yestarday = utilService.getYesterdayDateString();
logger.info("yestarday date: " + yestarday);

Document responseDocument = redirectResponse.parse();

FormElement searchForm = (FormElement) responseDocument.select("form[id=searchDisputesForm]").first();
checkElement("form element", searchForm);
FormElement form = (FormElement) searchForm;
Element searchField = form.select("input[name=DateFrom]").first();
checkElement("date from: ", searchField);
searchField.val(yestarday);
Element searchField1 = form.select("input[name=DateTo]").first();
checkElement("Date to: ", searchField1);
searchField1.val(yestarday);

Connection.Response searchResponse = form.submit()
        .cookies(coky)
        .sslSocketFactory(utilService.socketFactory())
        .userAgent(USER_AGENT)
        .method(Method.POST)
        .ignoreHttpErrors(true)
        .execute();
<!-- This is the HTML response that you get in the network -->
<form action="/123Mobile/Portal/Dispute/SearchDisputes" class="form-horizontal" enctype="multipart/form-data" id="searchDisputesForm" method="post">
    <input name="__RequestVerificationToken" type="hidden" value="ClhG-V26p7Aj3vi6W8tarCwHk_enagGT4mDVv2AZsU4MHATOBTbEQHuFmylooB5qvxF29aF1-wSvZZ26ijlcZJn5p_OSshL3KeqcEbAaYg01" />
    <!-- ... (other form elements) ... -->
    <button type="submit" class="btn btn-primary col-md-2 col-md-offset-1" id="SearchBtn" name="SearchBtn" formaction="/123Mobile/Portal/Reports/SearchTransactionReport">
        <span class="glyphicon glyphicon-search" aria-hidden="true"></span> Search
    </button>
</form>

Note: The HTML content has been retained as-is. If you need any further assistance or explanations, please let me know.

英文:

The data comes from a database, so how to parse HTML with this data, In my code, I connect to direct to my target page with cookies, then I connect to set search form, so how to parse with the result of the search. it comes from the backend. the code below contains my service connection code first, then the HTML of what the search form response gives, and finally, the html that appears in the network of the browser returning back after submitting the search form of the API post Method.

			Connection.Response redirectResponse = Jsoup.connect(internalConstant.getSearchResultURL())
.userAgent(USER_AGENT)
.sslSocketFactory(utilService.socketFactory())
.cookies(coky)
.method(Method.GET)
.execute();
//search file
String yestarday = utilService.getYesterdayDateString();
logger.info(&quot;yestarday date: &quot; + yestarday);
Document responseDocument = redirectResponse.parse();
FormElement searchForm = (FormElement) responseDocument.select(&quot;form[id=searchDisputesForm]&quot;).first();
checkElement(&quot;form element&quot;, searchForm);
FormElement form = (FormElement) searchForm;
Element searchField = form.select(&quot;input[name=DateFrom]&quot;).first();
checkElement(&quot;date from: &quot;, searchField);
searchField.val(yestarday);
Element searchField1 = form.select(&quot;input[name=DateTo]&quot;).first();
checkElement(&quot;Date to: &quot;, searchField1);
searchField1.val(yestarday);
Connection.Response searchResponse = form.submit()
.cookies(coky)
.sslSocketFactory(utilService.socketFactory())
.userAgent(USER_AGENT)
.method(Method.POST)
.ignoreHttpErrors(true)
.execute();
//and here is what gives me:
2020-10-15 11:37:38.193  INFO 3360 --- [http-nio-8095-exec-1] c.d.e.serviceImpl.UploaderTemplateImpl   : searchResponse :&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt; 
&lt;head&gt; 
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;&gt; 
&lt;title&gt;404 - File or directory not found.&lt;/title&gt; 
&lt;style type=&quot;text/css&quot;&gt;
&lt;!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:&quot;trebuchet MS&quot;, Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
--&gt;
&lt;/style&gt; 
&lt;/head&gt; 
&lt;body&gt; 
&lt;div id=&quot;header&quot;&gt;
&lt;h1&gt;Server Error&lt;/h1&gt;
&lt;/div&gt; 
&lt;div id=&quot;content&quot;&gt; 
&lt;div class=&quot;content-container&quot;&gt;
&lt;fieldset&gt; 
&lt;h2&gt;404 - File or directory not found.&lt;/h2&gt; 
&lt;h3&gt;The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.&lt;/h3&gt; 
&lt;/fieldset&gt;
&lt;/div&gt; 
&lt;/div&gt;  
&lt;/body&gt;
&lt;/html&gt;
//but the response in the network shows what I want:
&lt;form action=&quot;/123Mobile/Portal/Dispute/SearchDisputes&quot; class=&quot;form-horizontal&quot; enctype=&quot;multipart/form-data&quot; id=&quot;searchDisputesForm&quot; method=&quot;post&quot;&gt;&lt;input name=&quot;__RequestVerificationToken&quot; type=&quot;hidden&quot; value=&quot;ClhG-V26p7Aj3vi6W8tarCwHk_enagGT4mDVv2AZsU4MHATOBTbEQHuFmylooB5qvxF29aF1-wSvZZ26ijlcZJn5p_OSshL3KeqcEbAaYg01&quot; /&gt;                   
&lt;div class=&#39;col-md-12&#39;&gt;
&lt;div class=&#39;panel panel-grey equal-height-column&#39; id=&#39;&#39;&gt;
&lt;div class=&#39;panel-heading&#39;&gt;
&lt;h3 class=&#39;panel-title&#39;&gt;
&lt;i class=&#39;fa fa-tasks&#39;&gt;&lt;/i&gt; 
Search criteria
&lt;/h3&gt;
&lt;/div&gt;
&lt;div class=&#39;panel-body&#39;&gt;
&lt;div class=&#39;form-group&#39;&gt; &lt;label class=&quot;col-md-2 control-label&quot; for=&quot;DateFrom&quot;&gt;Date From&lt;/label&gt;
&lt;div class=&#39;col-md-4 col-md-offset-0&#39; id=&#39;&#39;&gt;
&lt;input class=&quot;form-control datepicker&quot; data-val=&quot;true&quot; data-val-date=&quot;The field Date From must be a date.&quot; id=&quot;DateFrom&quot; name=&quot;DateFrom&quot; placeholder=&quot;&quot; type=&quot;text&quot; value=&quot;2020-10-14&quot; /&gt;
&lt;span class=&quot;field-validation-valid text-danger&quot; data-valmsg-for=&quot;DateFrom&quot; data-valmsg-replace=&quot;true&quot;&gt;&lt;/span&gt;
&lt;/div&gt;&lt;label class=&quot;col-md-2 control-label&quot; for=&quot;DateTo&quot;&gt;Date To&lt;/label&gt;
&lt;div class=&#39;col-md-4 col-md-offset-0&#39; id=&#39;&#39;&gt;
&lt;input class=&quot;form-control datepicker&quot; data-val=&quot;true&quot; data-val-date=&quot;The field Date To must be a date.&quot; id=&quot;DateTo&quot; name=&quot;DateTo&quot; placeholder=&quot;&quot; type=&quot;text&quot; value=&quot;2020-10-14&quot; /&gt;
&lt;span class=&quot;field-validation-valid text-danger&quot; data-valmsg-for=&quot;DateTo&quot; data-valmsg-replace=&quot;true&quot;&gt;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;        
&lt;div class=&#39;col-md-12&#39;&gt;
&lt;div class=&#39; col-md-6&#39;&gt;
&lt;/div&gt;			
&lt;button type=&#39;button&#39; class=&#39;btn btn-success col-md-2 col-md-offset-1 &#39; id=&#39;ResetBtn&#39; name=&#39;ResetBtn&#39; onclick=&#39;&#39;&gt;
&lt;span class=&#39;glyphicon glyphicon-refresh&#39; aria-hidden=&#39;true&#39;&gt;&lt;/span&gt; Reset
&lt;/button&gt;
&lt;button type=&#39;submit&#39; class=&#39;btn btn-primary col-md-2 col-md-offset-1 &#39; id=&#39;SearchBtn&#39; name=&#39;SearchBtn&#39; formaction=&#39;/123Mobile/Portal/Reports/SearchTransactionReport&#39; onclick=&#39;&#39;&gt;
&lt;span class=&#39;glyphicon glyphicon-search&#39; aria-hidden=&#39;true&#39;&gt;&lt;/span&gt; Search
&lt;/button&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#39;col-md-12&#39;&gt;
&lt;div class=&#39;panel panel-grey equal-height-column&#39; id=&#39;&#39;&gt;
&lt;div class=&#39;panel-heading&#39;&gt;
&lt;h3 class=&#39;panel-title&#39;&gt;
&lt;i class=&#39;fa fa-tasks&#39;&gt;&lt;/i&gt; 
Search results
&lt;/h3&gt;
&lt;/div&gt;
&lt;div class=&#39;panel-body&#39;&gt;
&lt;div style=&#39;&#39; class=&#39;table-responsive&#39;&gt;&lt;table class=&#39;table table-hover table-striped&#39;&gt;&lt;thead&gt;&lt;tr&gt;&lt;th style=&#39;width:15%;&#39;&gt;File Name&lt;/th&gt;&lt;th style=&#39;width:10%;&#39;&gt;Scheme&lt;/th&gt;&lt;th style=&#39;width:15%;&#39;&gt;Proccessing Date&lt;/th&gt;&lt;th style=&#39;width:8%;&#39;&gt;&lt;/th&gt;&lt;th style=&#39;width:8%;&#39;&gt;&lt;/th&gt;&lt;/tr&gt; &lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&#39;-moz-word-break: break-all; -o-word-break: break-all; word-break: break-word;-ms-word-break: break-all;overflow-wrap: break-word;word-wrap: break-word; width: 15%;&#39; class=&#39;&#39;&gt;Transaction-FIBWallet-20201014&lt;/td&gt;&lt;td style=&#39;-moz-word-break: break-all; -o-word-break: break-all; word-break: break-word;-ms-word-break: break-all;overflow-wrap: break-word;word-wrap: break-word; width: 10%;&#39; class=&#39;&#39;&gt;FIBWallet&lt;/td&gt;&lt;td style=&#39;-moz-word-break: break-all; -o-word-break: break-all; word-break: break-word;-ms-word-break: break-all;overflow-wrap: break-word;word-wrap: break-word; width: 15%;&#39; class=&#39;&#39;&gt;2020-10-14&lt;/td&gt;&lt;td style=&#39;width: 8%;&#39; class=&#39;&#39;&gt;&lt;a href=&quot;/123Mobile/Portal/Reports/DownloadTransactionReport/40072&quot;&gt;Download as Text &lt;/a&gt;&lt;/td&gt;&lt;td style=&#39;width: 8%;&#39; class=&#39;&#39;&gt;&lt;a href=&quot;/123Mobile/Portal/Reports/DownloadTransactionReport/40073&quot;&gt;Download as Excel &lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#39;col-md-12&#39;&gt; &lt;div class=&quot;pagination-container&quot;&gt;&lt;ul class=&quot;pagination&quot;&gt;&lt;li class=&quot;disabled PagedList-skipToFirst&quot;&gt;&lt;a&gt;&#171;&#171;&lt;/a&gt;&lt;/li&gt;&lt;li class=&quot;disabled PagedList-skipToPrevious&quot;&gt;&lt;a rel=&quot;prev&quot;&gt;&#171;&lt;/a&gt;&lt;/li&gt;&lt;li class=&quot;active&quot;&gt;&lt;a&gt;1&lt;/a&gt;&lt;/li&gt;&lt;li class=&quot;disabled PagedList-skipToNext&quot;&gt;&lt;a rel=&quot;next&quot;&gt;&#187;&lt;/a&gt;&lt;/li&gt;&lt;li class=&quot;disabled PagedList-skipToLast&quot;&gt;&lt;a&gt;&#187;&#187;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;
&lt;/form&gt;

答案1

得分: 1

除非您需要从服务器获取会话 cookie 和一些其他信息(使用标头),否则我建议避免使用 GET 请求开始,并直接使用 POST 表单请求。

您说您收到的响应看起来类似于

&lt;h2&gt;404 - 文件或目录未找到。&lt;/h2&gt;

这表明您在进行 POST 请求时没有发送正确的标头,或者该页面是一个单页面应用程序,该应用程序会发出一些后续(异步)请求,然后修改页面源代码。

根据所提供的信息,我无法提供更多帮助,除了建议您使用 Chrome 开发工具上的网络选项卡,调查在发生 POST 请求时向服务器发送了哪些请求。查看标头。

感兴趣的典型标头涉及某种会话 cookie,可能还包括引荐者标头。但是,如果仅凭这两者不能满足您的需求,您将需要开始精确复制请求... 表单中可能有 CSRF 令牌,您需要获取这些令牌(在这种情况下,初始 GET 请求对于获取有效令牌以在 POST 中发送至关重要)。

英文:

Unless you need to get a session cookie and some other info from the server with headers, I'd avoid starting with a GET request and go straight for the POST form request.

You say the response you're getting looks something like

&lt;h2&gt;404 - File or directory not found.&lt;/h2&gt;

which suggests you're making the POST request without the correct headers sent across OR the page is a single page app that makes some subsequent (asynchronous) request which then modifies the page source.

I can't really help much further with the info provided, other than to say look at using Chrome dev tools on the network tab to investigate what requests are being sent to the server when the POST request happens. Look at the headers.

Typical headers of interest involve some sort of session cookie, possibly a referrer header. But if those two alone don't get you what you want you'll need to start replicating the request exactly... There may be CSRF tokens in the form you need to obtain (in which case the initial GET request is essential to obtain a valid token to send back in the POST).

huangapple
  • 本文由 发表于 2020年10月15日 18:31:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64369642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定