使用JSOUP从URL中提取内容

时间:2011-09-01 20:01:00

标签: java jsoup

我想从网址中提取内容,这是我想要使用jsoup提取的以下内容。

 And I want to extract text of <div id =content>

    <div id="content">
                  <!-- Start: Header -->
<div id="content-group_header-0" class="header content-podgroup-wrapper content-podgroup-0-0 first-pod">

    <h4 class="h4 no-font-replace ">
    Your imagination knows no borders or boundaries.  </h4>


  </div>
<!-- End: Header -->

<!-- Start: Open HTML -->
<div id="content-group_open_html-0" class="open-html wysiwyg content-podgroup-wrapper content-podgroup-0-0">

  <div class="open-html"><p>And we say, let it run wild. Because in that freedom is the shape of what’s to come.</p>
<p>Hello</p>
<p>Whatever it is you do, our pathway to innovation begins and ends with you.</p>
</div>
  </div>

<!-- End: Open HTML -->
<!-- Start: Header -->
<div id="content-group_header-1" class="header content-podgroup-wrapper content-podgroup-1-1">

    <h4 class="h4 no-font-replace ">
    Dreams grow, but not without nurturing.  </h4>

  </div>
<!-- End: Header -->

<!-- Start: Open HTML -->
<div id="content-group_open_html-1" class="open-html wysiwyg content-podgroup-wrapper content-podgroup-1-1">

  <p>In our fiscal year 2010, we invested 23% of our gross revenue or $2.549 billion in R&D—an investment that has increased every year since 2000. It’s how we’ve been able to stay ahead of the curve and deliver on the promise of new wireless technologies:</p>

<ul class="supporting-content">
<li> Hello World</li>

<li>We were the first to produce a single</li>

<li>We were the first to commercialize a chipset</li>

<li>We were the first to deliver GHz processing power integrated with 3G</li>

<li>We are the first to produce a laptop solution</li>

<li>We are the first to commercialize</li>

</ul>
<p>In fact, with a current intellectual property portfolio consisting of more than 77,000 patents granted and pending</p>
  </div>
<!-- End: Open HTML -->
<!-- Start: Header -->
<div id="content-group_header-2" class="header content-podgroup-wrapper content-podgroup-2-2">

    <h4 class="h4 no-font-replace ">
    Our partnerships are our most valuable assets.  </h4>

  </div>
<!-- End: Header -->

<!-- Start: Open HTML -->
<div id="content-group_open_html-2" class="open-html wysiwyg content-podgroup-wrapper content-podgroup-2-2">

<p></p>
<p></p>
</div>

  </div>
<!-- End: Open HTML -->
<!-- Start: Header -->
<div id="content-group_header-3" class="header content-podgroup-wrapper content-podgroup-3-3">

    <h4 class="h4 no-font-replace ">
    So where does the pathway to innovation lead?  </h4>

  </div>
<!-- End: Header -->

<!-- Start: Open HTML -->
<div id="content-group_open_html-3" class="open-html wysiwyg content-podgroup-wrapper content-podgroup-3-3 last-pod">

  <div class="open-html"><p>We’re eager to find out ourselves, because if the pathway begins with you, then it’s up to you to decide where it’s headed. So let’s explore the possibilities. Let’s never stop discovering.</p>
<p>Working together, the pathway to innovation can go as far as our dreams will take us.</p>
</div>
  </div>
<!-- End: Open HTML -->
                </div><!-- END: content -->

任何人都可以解释如何使用JSOUP来完成。

1 个答案:

答案 0 :(得分:4)

为什么要为此使用正则表达式? Jsoup是一个只解析CSS selectors的HTML解析器。只需使用适当的CSS选择器。我了解您要选择<div id="content">?请使用此格式#id的CSS ID选择器。

Document document = Jsoup.connect("http://www.host.com/domain").get();
Element content = document.select("#content").first();
System.out.println(content.html()); 
// ...

或者,如果您只想获取文字,请使用Element#text()代替Element#html()

System.out.println(content.text()); 
// ...

彻底阅读以下文档,了解如何在Jsoup中使用CSS选择器:

You shouldn't think about using regexps to parse HTML