如何在< p>之间提取文本标签 [英] How to extract texts between <p> tags

查看:102
本文介绍了如何在< p>之间提取文本标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从放置在 p li 标签中的HTML页面提取文本,所以我可以开始标记页面,为每个页面构建倒排索引,以回答搜索查询。



我如何获得 p tags使用jsoup

 元素e = doc.select(); 

可以在该参数中写入字符串

解决方案

这可以完成这项工作

 元素e = doc。选择( p); 

以下是所有选择器

假设您有这个html:

 字符串html =< p>某些< strong>粗体< / strong>文字< / p>; 

要获得一些粗体文本应该使用:

  Document doc = Jsoup.parse(html); 
元素p = doc.select(p)。first();
String text = doc.body()。text(); //一些粗体文本

  String text = p.text(); //一些粗体文本

现在假设您有以下复杂html

  String html =< div id = someid>< p>一些文字< / p>< span>其他一些文字< / span>< p>另一个p标签< / p>< / div> 

要从两个 p 标签你必须做这样的事情

  Document doc = Jsoup.parse(html); 
元素内容= doc.getElementById(someid);
元素p = content.getElementsByTag(p);

String pConcatenated =;
for(Element x:p){
pConcatenated + = x.text();
}

System.out.println(pConcatenated); // sometext another p tag

您还可以在此处找到更多信息。



希望这有助于

I want to extract texts from HTML page(s) which placed in p and li tags, so I can start to tokenize the page to construct inverted index(es) for each page in order to answer search queries.

How I can get p tags using jsoup

Elements e = doc.select(""); 

What could be the string to be written in that parameter?

解决方案

This can do the job

Elements e=doc.select("p"); 

Here is a list of all selectors you can use.

Suppose you have this html:

String html="<p>some <strong>bold</strong> text</p>";

To get some bold text as result you should use:

Document doc = Jsoup.parse(html);
Element p= doc.select("p").first();
String text = doc.body().text(); //some bold text

or

String text = p.text(); //some bold text

Suppose now you have the following complex html

String html="<div id=someid><p>some text</p><span>some other text</span><p> another p tag</p></div>"

To get the values from the two p tags you have to do something like this

Document doc = Jsoup.parse(html);
Element content = doc.getElementById("someid");
Elements p= content.getElementsByTag("p");

String pConcatenated="";
for (Element x: p) {
  pConcatenated+= x.text();
}

System.out.println(pConcatenated);//sometext another p tag

You can find more info here also

Hope this helped

这篇关于如何在&lt; p&gt;之间提取文本标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆