使用jsoup从两个标签之间提取未识别的html内容?正则表达式

时间:2015-04-22 10:02:00

标签: java html parsing jsoup wikipedia

我想从那里的两个h2标签中获取所有这些链接的名称

<h2><span class="mw-headline" id="People">People</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Bush&amp;action=edit&amp;section=1" title="Edit section: People">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
<ul>
<li><a href="/wiki/George_H._W._Bush" title="George H. W. Bush">George H. W. Bush</a> (born 1924), the 41st president of the United States of America</li>
<li><a href="/wiki/George_W._Bush" title="George W. Bush">George W. Bush</a> (born 1946), the 43rd president of the United States of America</li>
<li><a href="/wiki/Jeb_Bush" title="Jeb Bush">Jeb Bush</a> (born 1953), the former governor of Florida and also a member of the Bush family</li>
<li><a href="/wiki/Bush_family" title="Bush family">Bush family</a>, the political family that includes both presidents</li>
<li><a href="/wiki/Bush_(surname)" title="Bush (surname)">Bush (surname)</a>, a surname (including a list of people with the name)    </li>
</ul>
<h2><span class="mw-headline" id="Places.2C_United_States">Places, United States</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Bush&amp;action=edit&amp;section=2" title="Edit section: Places, United States">edit</a><span class="mw-editsection-bracket">]</span></span></h2>

这两个

    Elements h2next = docx.select("span.mw-headline#People");
    do 
    {
     ul = h2next.select("ul").first();
     System.out.println(ul.text());
    } 
    while (h2next!=null && ul==null);

也不是

    //String content = docx.getElementById("People").outerHtml();

作品。

似乎this guy,有正确的想法,但我不能让它适应我的情况。

也许我应该使用正则表达式?

似乎维基百科html有点&#34;非结构化&#34;而且难以合作。

the wikipedia disambiguation page我想抓住不同的感官,其中Bush(或我考虑的任何模棱两可的名字)都可以用作一个人。

我已经尝试过各种方法来使用jsoup来获取这些数据,但我还没弄清楚。

我试过了:

Document docx = Jsoup.connect("https://en.wikipedia.org/wiki/Bush").get();
Element contentDiv = docx.select("span#mw-headlinePeople").first();
String printMe = contentDiv.toString(); // The result

因为我注意到我想要的数据存在于名为:

的分区中
 <h2><span class="mw-headline" id="People">

但是输出没什么。

我根据之前的问题尝试了一些变体:

.select("span#mw-headlinePeople");

但仍然没有。

如何获取该信息?

理想情况下,我喜欢的是这样的:

George H. W. Bush 
George W. Bush 
Jeb Bush 

虽然我知道我可能最初也必须得到Bush familyBush (surname),因为他们是该细分受众群的一部分,但我想我可以稍后删除它们。

此外,使用它更快:

Document docx = Jsoup.connect("https://en.wikipedia.org/wiki/Bush").get();

或者这个:

    URL site_two = new URL("https://en.wikipedia.org/wiki/Bush");

    URLConnection ycb = site_two.openConnection();
    BufferedReader inb = new BufferedReader(
                            new InputStreamReader(
                            ycb.getInputStream()));

    StringBuilder sb = new StringBuilder();

    while ((inputLine = inb.readLine()) != null) 
    {
        //get the disambig
        //System.out.println(inputLine);

        sb.append(inputLine);
        sb.append(System.lineSeparator());
        inputLine = inb.readLine();
    }

我尝试使用this site,但事实证明它不是很有用。有人应该像所有这些正则表达式网站一样建立一个jsoup网站。

2 个答案:

答案 0 :(得分:3)

一种可能的方法是同时选择所有标题(span.mw-headlines)和所有链接(我找到的最佳选择器为li > a)。

如果您通过将一个选择器与,组合在一起来选择两个选择器,则它们将按照它们在页面上显示的顺序排列。因此,您可以在循环搜索结果时跟踪您是否处于“人员”部分:

Elements elements = docx.select("span.mw-headline, li > a");

boolean inPeopleSection = false;
for (Element elem : elements) {
    if (elem.className().equals("mw-headline")) {
        // It's a headline
        inPeopleSection = elem.id().equals("People");
    } else {
        // It's a link
        if (inPeopleSection) {
            System.out.println(elem.text());
        }
    }
}

输出:

George H. W. Bush
George W. Bush
Jeb Bush
Bush family
Bush (surname)

关于性能,我认为它没有任何区别,只需使用更简单的版本(虽然我的Jsoup经验非常有限,所以不要相信我的话)。

答案 1 :(得分:1)

一个简单的选择器是h2:contains(people) + ul a,例如:

Elements els = doc.select("h2:contains(people) + ul a");

这给出了以下要素:

0 <a href="/wiki/George_H._W._Bush" title="George H. W. Bush">
George H. W. Bush
1 <a href="/wiki/George_W._Bush" title="George W. Bush">
George W. Bush
2 <a href="/wiki/Jeb_Bush" title="Jeb Bush">
Jeb Bush
3 <a href="/wiki/Bush_family" title="Bush family">
Bush family
4 <a href="/wiki/Bush_(surname)" title="Bush (surname)">
Bush (surname)

我使用了try.jsoup.org(请参阅working example)和selector syntax guide作为资源。