title: How to extract headline titles followed by respective text from Wikipedia
body: <p>I am trying to use Jsoup in order to extract text from Wikipedia articles.</p>
  
  <p>My idea is to simply extract every headline, and their respective text paragraphs.</p>
  
  <p>I am having some trouble understanding how I can take only the specific text of each section, here's what I have:</p>
  
  <pre><code>public static void main(String[] args) {
  
  String url = "http://en.wikipedia.org/wiki/Albert_Einstein";
  Document doc;
  
  try {
      doc = Jsoup.connect(url).get();
      doc = Jsoup.parse(doc.toString());
  
      Elements titles = doc.select(".mw-headline");
      PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
      System.setOut(out);
  
      for(Element h3 : doc.select(".mw-headline")) 
      {
          String title = h3.text();
  
          String titleID = h3.id();
  
          Elements paragraphs = doc.select("p#"+titleID);
  
          //Element  nextEle=h3.nextElementSibling();
  
          System.out.println(title);
          System.out.println("----------------------------------------");
          System.out.println(titleID);
          System.out.print("\n");
          System.out.println(paragraphs.text());
          System.out.print("\n");
      }
  
  } catch (IOException e) {
      System.out.println("deu merda");
      e.printStackTrace();
  }
  </code></pre>
  
  <p>With this I can extract every headline, but I can't get how I would get the text from each section to print it accordingly. I was thinking maybe with the headline's ID, but no dice.</p>
  
  <p>Thank you for any help!</p>