2017-05-17 1 views
0

Ich muss Web-Scraper-Dienstprogramm erstellen, die Web-Ressourcen per URL erhalten. Zählen Sie dann die Anzahl der Wortvorkommen auf der Webseite und die Anzahl der Zeichen.Parse HTML (Web-Seite) JavaSE

URL url = new URL(urlStr); 
URLConnection connection = url.openConnection(); 
InputStream inputStream = connection.getInputStream(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8")); 

Damit ich den gesamten Text auf der Seite (und HTML-Tags) bekommen so, was ich als nächstes tun?

Kann mir jemand dabei helfen? Etwas Doc oder Sthg zum Lesen. Ich brauche nur JavaSE. Die 3D-Party-Bibliothek kann nicht verwendet werden.

+1

warum genau? Bei so vielen Bibliotheken * ist das Rad neu zu erfinden in der Regel eine schlechte Wahl. –

+0

@Shashwat Ich verstehe das und weiß über jsoup und andere. Aber es ist ein Testfall. Sie sagen "Tipps: - Verwenden Sie keine Drittanbieter-Bibliotheken" und ich stimme Ihnen zu. Nach 5 Stunden fand ich keine gute Antwort für diese Aufgabe. –

+0

Durch HTMLEditorKit versucht, aber ist das richtig? –

Antwort

0

Zum Beispiel haben Sie seite.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
     <title>Login Page</title> 
    </head> 
    <body> 
     <div id="login" class="simple" > 
      <form action="login.do"> 
       Username : <input id="username" type="text" /> 
       Password : <input id="password" type="password" /> 
       <input id="submit" type="submit" /> 
       <input id="reset" type="reset" /> 
      </form> 
     </div> 
    </body> 
</html> 

es zu analysieren, können Sie mit:

import java.io.File; 
import java.io.IOException; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 

/** 
* Java Program to parse/read HTML documents from File using Jsoup library. 
*/ 
public class HTMLParser{ 

    public static void main(String args[]) { 

     // Parse HTML String using JSoup library 
     String HTMLSTring = "<!DOCTYPE html>" 
       + "<html>" 
       + "<head>" 
       + "<title>JSoup Example</title>" 
       + "</head>" 
       + "<body>" 
       + "<table><tr><td><h1>HelloWorld</h1></tr>" 
       + "</table>" 
       + "</body>" 
       + "</html>"; 

     Document html = Jsoup.parse(HTMLSTring); 
     String title = html.title(); 
     String h1 = html.body().getElementsByTag("h1").text(); 

     System.out.println("Input HTML String to JSoup :" + HTMLSTring); 
     System.out.println("After parsing, Title : " + title); 
     System.out.println("Afte parsing, Heading : " + h1); 

     // JSoup Example 2 - Reading HTML page from URL 
     Document doc; 
     try { 
      doc = Jsoup.connect("http://google.com/").get(); 
      title = doc.title(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     System.out.println("Jsoup Can read HTML page from URL, title : " + title); 

     // JSoup Example 3 - Parsing an HTML file in Java 
     //Document htmlFile = Jsoup.parse("login.html", "ISO-8859-1"); // wrong 
     Document htmlFile = null; 
     try { 
      htmlFile = Jsoup.parse(new File("login.html"), "ISO-8859-1"); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } // right 
     title = htmlFile.title(); 
     Element div = htmlFile.getElementById("login"); 
     String cssClass = div.className(); // getting class form HTML element 

     System.out.println("Jsoup can also parse HTML file directly"); 
     System.out.println("title : " + title); 
     System.out.println("class of div tag : " + cssClass); 
    } 
} 

Ausgang:

Input HTML String to JSoup :<!DOCTYPE html><html><head><title>JSoup Example</title></head><body><table><tr><td><h1>HelloWorld</h1></tr></table></body></html> 
After parsing, Title : JSoup Example 
Afte parsing, Heading : HelloWorld 
Jsoup Can read HTML page from URL, title : Google 
Jsoup can also parse HTML file directly 
title : Login Page 
class of div tag : simple 
+0

Das OP sagt ausdrücklich * Kann nicht 3D-Party-Bibliothek verwenden. * –

+0

Ok, verstanden, ich werde nur einmal sehen –