简体   繁体   English

如何使用jericho html解析器从网站获取数据?

[英]How to fetch data from a website using jericho html parser?

I am using jericho html parser in java. 我在Java中使用jericho html解析器。 I want to fetch data from a website. 我想从网站获取数据。 In website html content is like this.... 网站上的html内容就是这样。

<div class="class_div">
   <div class="class_div2">All contents...</div>`
     <span class="equals">Content 1</span>
     <span class="equals">Content 2</span>
     <span class="equals">Content 3</span>
     <span class="equals">Content 4</span>
 </div>

I want to fetch Content 1,Content 2, Content 3, Content 4. How to fetch this? 我想获取内容1,内容2,内容3,内容4。如何获取此内容?

I am using this code 我正在使用此代码

String sourceUrlString="<website url>";
if (sourceUrlString.indexOf(':')==-1)
sourceUrlString="http:"+sourceUrlString;
Source source=new Source(new URL(sourceUrlString));
Element bodyContent = source.getElementByClass("equals");`

Where's the Problem? 问题出在哪里? With your code you get each Element - with those you get their text: 有了您的代码获取每个Element -与你自己的文字:

Source source = new Source(/* ... */);
List<Element> elements = source.getAllElementsByClass("equals");

for( Element element : elements )
{
    /*
     * 'element.getTextExcrator().toString()' returns the text of the element
     */
    System.out.println(element.getTextExtractor().toString());
}

Output: 输出:

Content 1 内容1
Content 2 内容2
Content 3 内容3
Content 4 内容4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM