Results 1 to 7 of 7

Thread: Parse Html

  1. #1
    Aleksa is offline Member
    Join Date
    Jun 2014
    Posts
    4
    Rep Power
    0

    Default Parse Html

    Hello! I do paper work and I need help. With this code I take it data from a webpage, I get HTML response. How to get clean data? How to use jsoup for parsing HTML in this code? Тhanks in advance. :)

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;

    /**
    *
    * @author Aleksandra
    */
    public class Main {

    /**
    * @param args the command line arguments
    */
    public void sendPostRequest() {

    //Build parameter string
    String data = "verbe=ѕвони";
    try {

    // Send the request
    URL url = new URL("http://fleximac.free.fr/mkd/1conj.php");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

    //write parameters
    writer.write(data);
    writer.flush();

    // Get the response
    StringBuffer answer = new StringBuffer();
    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = reader.readLine()) != null) {
    answer.append(line);
    }
    writer.close();
    reader.close();

    //Output the response
    System.out.println(answer.toString());

    } catch (MalformedURLException ex) {
    ex.printStackTrace();
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    /**
    * Starts the program
    *
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    new Main().sendPostRequest();
    }

    }

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,828
    Rep Power
    19

    Default Re: Parse Html

    The jsoup docs are here.
    And this is the cookbook, with lots of examples.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Aleksa is offline Member
    Join Date
    Jun 2014
    Posts
    4
    Rep Power
    0

    Default Re: Parse Html

    Quote Originally Posted by Aleksa View Post
    Hello! I do paper work and I need help. With this code I take it data from a webpage, I get HTML response. How to get clean data? How to use jsoup for parsing HTML in this code? Тhanks in advance. :)

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;

    /**
    *
    * @author Aleksandra
    */
    public class Main {

    /**
    * @param args the command line arguments
    */
    public void sendPostRequest() {

    //Build parameter string
    String data = "verbe=ѕвони";
    try {

    // Send the request
    URL url = new URL("http://fleximac.free.fr/mkd/1conj.php");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

    //write parameters
    writer.write(data);
    writer.flush();

    // Get the response
    StringBuffer answer = new StringBuffer();
    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = reader.readLine()) != null) {
    answer.append(line);
    }
    writer.close();
    reader.close();

    //Output the response
    System.out.println(answer.toString());

    } catch (MalformedURLException ex) {
    ex.printStackTrace();
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    /**
    * Starts the program
    *
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    new Main().sendPostRequest();
    }

    }
    I have read jsoup documents. But I do not know how to use this library in my code to get the text from tags for column (td). Here's what it looks html table I receive as response from web page. Can you help me with the code?


    <table align="center" border="1">
    <tr>
    <td border="0">
    <td bgcolor="#99B3FF" align="center">
    <b>Еднина</b>
    </td>
    <td bgcolor="#99B3FF" align="center">
    <b>Множина</b>
    </td>
    </tr>
    <tr>
    <td bgcolor="#99B3FF">
    <b>1 лице</b>
    </td>
    <td>јас играм
    <br>
    </td>
    <td>ние играмe
    <br>
    </td>
    </tr>
    <tr>
    <td bgcolor="#99B3FF">
    <b>2 лице</b>
    </td>
    <td>ти играш
    <br>
    </td>
    <td>вие играте
    <br>
    </td>
    </tr>
    <tr>
    <td bgcolor="#99B3FF">
    <b>3 лице</b>
    </td>
    <td>тој/таа/тоа игра
    <br>
    </td>
    <td>тие играат
    <br>
    </td>
    </tr>
    </table>

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Parse Html

    Make an attempt. Show your code. Show the error. This is not a CodeFactory™
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,828
    Rep Power
    19

    Default Re: Parse Html

    Quote Originally Posted by Aleksa View Post
    I have read jsoup documents. But I do not know how to use this library in my code to get the text from tags for column (td). Here's what it looks html table I receive as response from web page. Can you help me with the code?
    As SurfMan says, you need to actually try.
    You haven't even imported the relevant classes!
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Aleksa is offline Member
    Join Date
    Jun 2014
    Posts
    4
    Rep Power
    0

    Default Re: Parse Html

    I will import the appropriate classes. But I do not know how to use the methods of jsoup in my code to get only plain text from html.

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,828
    Rep Power
    19

    Default Re: Parse Html

    You clearly haven't read through my links.

    "Extract attributes, text, and HTML from elements"
    In the section "Extracting data" of the cookbook link I gave above!
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Best way to parse HTML??
    By SeeD419 in forum New To Java
    Replies: 5
    Last Post: 07-10-2011, 05:05 PM
  2. How to parse a html document
    By Habib in forum Networking
    Replies: 1
    Last Post: 05-01-2011, 08:07 PM
  3. Parse HTML
    By gab in forum New To Java
    Replies: 1
    Last Post: 02-21-2011, 10:53 PM
  4. How to parse in html
    By paty in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 12:29 AM
  5. How to parse HTML tags
    By Ada in forum Advanced Java
    Replies: 1
    Last Post: 05-31-2007, 09:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •