JAVA Implementation of Baidu Post Bar "only look at the landlord"

Source: Internet
Author: User

This function is mainly used: first determine the post address, then obtain the HTML code, parse the HTML through jsoup, and then extract the building owner to save it to book.html.

In addition to the use of jsoup, we should look at the general structure of the post:

1. Analyze the User Name of the landlord first:

If you look at the HTML code, you can know that the user name is in the DIV of class = "p_author_name". The landlord is of course the first, so:

String LZ = Doc. getelementsbyclass ("p_author_name" (.get(0).html ();

2. The body of the reply is found in the DIV class = "p_postlist". First, extract this element E = Doc. getelementsbyclass ("p_postlist"). Get (0 );

3. retrieve each reply class = "l_post" and traverse

Elements es = E. getelementsbyclass ("l_post"); // retrieves each record for (element TMP: ES) {// traverses if (LZ. equals (TMP. getelementsbyclass ("p_author_name" ).html () {// save it as string html = TMP. getelementsbyclass ("d_content" (.get(0).html (); html = html. replaceall ("

4. Determine whether the post has reached the end page

Each post contains two pages. Take out any one of them and judge whether it contains the "next page". If it is gone, it is the last page.

5. Save it.

Because we only extract the content in the DIV, it is a bit messy to save. You can write a CSS file and then save it.

Complete code:

Private void see (string URL) {try {int page = 0; stringbuffer sb = new stringbuffer (); document DOC = jsoup. connect (URL ). get (); string LZ = Doc. getelementsbyclass ("p_author_name" (.get(0).html (); // obtain the username of the first user, that is, the main user elements posts = NULL; do {Element E = Doc. getelementsbyclass ("p_postlist "). get (0); // get the divelements es = E. getelementsbyclass ("l_post"); // retrieves each record for (element TMP: ES) {// traverses if (LZ. equals (TMP. getelementsbyclass ("p_author_name" ).html () {string html = TMP. getelementsbyclass ("d_content" (.get(0).html (); html = html. replaceall ("

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.