Android:httpwatch+jsoup Grab Bag

Source: Internet
Author: User

Recently, I was interested in catching the bag, so it took a little time to study it. Finally, summarize this approach.

HttpWatch

This software is equivalent to ie a plugin (Firefox also support). HttpWatch is a powerful web data analysis tool, integrated in the Internet Explorer toolbar, including Web snippets, cookie management, cache management, message hair/reception, character query, POST data and directory management functions, report output and more. In short, HttpWatch is a software that collects and displays deep information.

For the use of HttpWatch can be consulted: HttpWatch tool Introduction and use of skills

Jsoup

Jsoup is a Java HTML parser that can parse a URL address and HTML text content directly. It provides a very labor-saving API that can be used to extract and manipulate data through dom,css and jquery-like operations.

In summary, here Jsoup is used to parse the HTML code returned by the server.

Download the Jsoup
Selector of Jsoup

Combat once

Crawl target: On-campus news website

    • 1. Install HttpWatch, open IE right-click Web page or select toolbar to open HttpWatch

    • 2. Locate the target website where we want to crawl the data, HttpWatch Click Record, go to the target website, HttpWatch click Stop. So we've got the first data.

    • 3. Analyze the data: From the can know, this is a httpget request, so we write a HttpGet method, in the overview column can see the requested URL is:
      Http://notes.stu.edu.cn/login/Login.jsp?logintype=1 (just demo function, outside the network is dot not in)

     Public Static void sendhttpgetrequest(FinalString URL,FinalHttpcallback callBack) {NewThread (NewRunnable () {@Override             Public void Run() {//TODO auto-generated method stubHttpClient client =NewDefaulthttpclient (); HttpGet HttpGet =NewHttpGet (URL);//Httpget.setheader ("Cookie", Cookie_grade);HttpResponse response =NULL;Try{response = Client.execute (HttpGet);                    httpentity entity = response.getentity (); String string = entityutils.tostring (Entity,"Utf-8");if(CallBack! =NULL) callback.onfinish (string); }Catch(Clientprotocolexception e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace ();    }}). Start (); }
    • 4. No surprises, our callback method will get the HTML code returned by the server. Then we'll use Jsoup to parse the HTML code, and we can look at the contents of the HTML in the HttpWatch.

      The message we want is this piece of HTML code snippet
<TR class=datalight>                           <TD width="50%"><a target =' _blank 'href= '/page/maint/template/news/ newstemplateprotal.jsp?templatetype=1&templateid=3&docid=2382 ">  <img class="vt" src="/csweb/images/38.jpg">&AMP;NBSP;&AMP;NBSP;36 hours to meet your five desires ―&lt; creative hack loose activity data Life Chapter &gt; 20150327-29</a></TD>                            <TD width="25%" align="center">Network and Information Center</TD>                            <TD width="25%" align="center">2015-04-10</TD>                  </TR>                  <TR class=datalight>                           <TD width="50%"><a target =' _blank 'href= '/page/maint/template/news/ newstemplateprotal.jsp?templatetype=1&templateid=3&docid=2381 ">  <img class="vt" src="/csweb/images/38.jpg">&nbsp;&nbsp; Notice of the traditional handicraft exploration activities in Shantou for the student Community X program</a></TD>                            <TD width="25%" align="center">Student Office</TD>                            <TD width="25%" align="center">2015-04-10</TD>                  </TR>

So just write an analytic approach based on selector of Jsoup

    Private Map<String,String>Parsenotehtml (StringHTML) {Map<String,String> Map = NewLinkedhashmap<String,String>(); Document doc=Jsoup.Parse (HTML); Elements Elements=Doc.Select("Table[class=liststyle]"); Elements elements2=Elements.Select("Tr[class=datalight]"); for (Element element:elements2) {Elements element_href=Element.Select("a"); For (Element Element_: element_href) {Map.Put (element.Text (), Element_.attr"href")); }        }return Map; }

You can see that we also parse the file name link that each message carries.
for (Element element_ : element_href) {
map.put(element.text(), element_.attr("href"));
}

    • 5. We click on the specific news of the campus news network,

      The URL that can be found for specific news is a hostname + a link to the file name we parse. Let's grab the URL and get the news content.
Results
    • News articles

    • Specific News

The final note: The above-mentioned capture process is HttpGet request, for HttpPost request is also similar, to analyze more data.

Android:httpwatch+jsoup Grab Bag

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.