2016-05-14 2 views
0

Ich habe eine dieser html:Wie ein document.write Wert in jsoup HTML-Analyse erhalten

<html> 
<head> 
<title>Try jsoup</title> 
</head> 
<body class="sin"> 
<div class="ks"> 
    <div class="wrap"> 

     <div class="mag-right-sidebar-wrap"> 
      <main class="mag"> 

       //A lot of unneeded tags 

       <article class="post-1989009 post type-post post" itemscope="" itemtype="http://schema.org/CreativeWork"> 
        <header class="post-header"> 
         <h1 class="post-title" itemprop="headline">Knowledge nay</h1> 
         <img src="https://ohniee.com/wp-mag/uploads/avatars/1/djsy8933e89ufio8389e8-author-img.jpg" class="avatar user-1-avatar avatar-40 photo" width="40" height="40" alt="Profile photo of Johnnie Adams"> 

         <div class="flip-meta" style="padding-top:3px; margin-left: 50px"> 
lorem ipsum <a href="/members/iyke"><span class="flip-author" itemprop="author" itemscope itemtype="http://schema.org/Person"><span class="flip-author-name" itemprop="name"> Johnnie Adams</span></span></a> <script> 
document.write(" on June 1st, 2005 00:99 ")</script> . <span class="flip-comments-link"><a href="https://ohniee.com/lorem-ipsum">25 Comments</a></span> 
</div> 
        </header> 

        //A lot of unneeded tags 
</body> 
</html> 

und ich versuche Lorem ipsum Johnnie Adams am 1. Juni zu extrahieren, 2005 00:99 davon . Aber was ich bekomme ist Lorem Ipsum Johnnie Adams. 25 Kommentare.

Bitte, wie bekomme ich Lorem Ipsum Johnnie Adams am 1. Juni 2005 00:99 aus dem HTML?

Dies ist der Code, den ich

document.select("div.flip-meta").first().text(); 

Jsoup Demo Link mithilfe bin: https://try.jsoup.org/~BAit4PmvqNcdVAKLBv4Yp4QrXYQ

+0

Mögliches Duplikat von [Java - Erhalte Text innerhalb des Script-Tags mit Hilfe von Jsoup] (http://stackoverflow.com/questions/16780517/java-obtain-text-within-script-tag-using-jsoup) – Stephan

Antwort

1

Ändern Stephens Antwort,

Element script = document.select("div.flip-meta script").first(); 
if (script==null) { 
    throw new RuntimeException("script element not found"); 
} 

String scriptContent = script.html().replace("document.write(\"", "").replace("\")", ""); 

String text1 = document.select("div.flip-meta").first().text(); 
String text2 = text1.replaceAll("\\s*[.?!].*",""); 

String finaltext = text2 + scriptContent; 

urTextView.setText(finaltext); 

Lorem ipsum Johnnie Adams erhalten Sie auf Dies sollte 1. Juni 2005 00:99

+0

Arbeitete aber was macht '(" \\ s * [.?!]. * "," ") bedeuten? – Roseyk

+0

Es ist eine Regex. Es wählt den Abstand vor dem Punkt und den Text nach dem Punkt aus. Also 'replaceAll (" \\ s * [.?!]. * "," ")' Wählt ** aus. 25 Kommentare ** und ändert es in leeren Text. – X09

+0

Ok. Danke für die Erklärung. – Roseyk