如何获得一个“a href” jSoup在一个html类中使用了很多 [英] How to get one "a href" out of many in one html class with jSoup

查看:96
本文介绍了如何获得一个“a href” jSoup在一个html类中使用了很多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将HTML中的所有文本元素提取到Java字符串中。但是在单独的字符串中。

我有以下代码:

  < div class =sb-spieldaten> 

< a href =/ jumplist / spieltag / wettbewerb / C1 / saison_id / 2014 / spieltag / 2> 2。 Spieltag< / A>
& nbsp;& nbsp; |& nbsp;& nbsp;
< a href =/ aktuell / waspassiertheute / aktuell / new / datum / 2014-07-26> Sa。,26.07.2014< / a>
& nbsp;& nbsp; |& nbsp;& nbsp; 17:45 Uhr
< / p>

< a href =/ jumplist / spieltag / wettbewerb / C1 / saison_id / 2014 / spieltag / 2> 2。 Spieltag< / A>
< br />
< a href =/ aktuell / waspassiertheute / aktuell / new / datum / 2014-07-26> 26.07.2014< / a>
< br>
17:45 Uhr
< / p>
< div class =ergebnis-wrap>
< div class =sb-ergebnis>
< div class =sb-endstand> 2:3
< div class =sb-halbzeit>(< span> 2:< / span> 2)
< / div>
< / div>
< / div>
< / div>
< p class =sb-zusatzinfos>
< a href =/ stadion / stadion / verein / 504 / saison_id / 2014> Letzigrund< / a>
& nbsp;& nbsp; |& nbsp;& nbsp;
< strong> 4.200 Zuschauer< / strong>
< br />
< / span>
< strong> Schiedsrichter:< / strong>
< br class =show-for-small/>
< a title =Fedayi Sanhref =/ fedayi-san / profil / schiedsrichter / 4791> Fedayi San< / a>
< / p>
< / div>

我用:

 元素myText = doc.getElementsByClass(sb-spieldaten); 
String myString = myText.select(a.sb-datum.hide-for-small).text();

但是通过这个,我提取了hide-for-small类中的所有字符串,所以我得到的答案是:2. Spieltag | Sa.,26.07.2014 | 17:45 Uhr 2。 Spieltag 26.07.2014 17:45 Uhr Letzigrund | 4200 Zuschauer Schiedsrichter:Fedayi San



如何获得这个字符串中的一个?我无法通过.getElementsByClass找到它(...)是可以理解的。有没有办法提取特定的a href元素?或者我必须使用.split()方法吗?

解决方案

代码片段例如

 文档abc = Jsoup.connect(http:/ /www.abc.in /)。timeout(0).get(); 
元素ee = abc.select(a [href * = xyz]); //所有包含xyz子字符串的hrefs
String xyz = ee.first()。attr(abs:href);


I have to extract all text elements in HTML into Java Strings. But in seperate Strings.

I have the following code:

<div class="sb-spieldaten">
    <p class="sb-datum hide-for-small">
        <a href="/jumplist/spieltag/wettbewerb/C1/saison_id/2014/spieltag/2">2. Spieltag</a>
        &nbsp;&nbsp;|&nbsp;&nbsp;
        <a href="/aktuell/waspassiertheute/aktuell/new/datum/2014-07-26">Sa., 26.07.2014</a>
        &nbsp;&nbsp;|&nbsp;&nbsp;17:45 Uhr
    </p>
    <p class="sb-datum show-for-small">
        <a href="/jumplist/spieltag/wettbewerb/C1/saison_id/2014/spieltag/2">2. Spieltag</a>
        <br />
        <a href="/aktuell/waspassiertheute/aktuell/new/datum/2014-07-26">26.07.2014</a>
        <br>
        17:45 Uhr
    </p>
    <div class="ergebnis-wrap">
        <div class="sb-ergebnis">
            <div class="sb-endstand">2:3
                <div class="sb-halbzeit">(<span>2:</span>2)
                </div>
            </div>
        </div>
    </div>
    <p class="sb-zusatzinfos">
        <span class="hide-for-small">
            <a href="/stadion/stadion/verein/504/saison_id/2014">Letzigrund</a>
            &nbsp;&nbsp;|&nbsp;&nbsp;
            <strong>4.200 Zuschauer</strong>
            <br />
        </span>
        <strong>Schiedsrichter:</strong>
        <br class="show-for-small" />
        <a title="Fedayi San" href="/fedayi-san/profil/schiedsrichter/4791">Fedayi San</a>
    </p>
</div>

I use:

Elements myText = doc.getElementsByClass("sb-spieldaten");
String myString = myText.select(a.sb-datum.hide-for-small").text();

But with this I extract all Strings in the class "hide-for-small". So the answer I get is : 2. Spieltag | Sa., 26.07.2014 | 17:45 Uhr 2. Spieltag 26.07.2014 17:45 Uhr Letzigrund | 4200 Zuschauer Schiedsrichter: Fedayi San

How do I get only one of this Strings? I can't find it with .getElementsByClass("...") understandably. Is there a way to extract a specific "a href" element? Or do I have to use the .split() method?

解决方案

Code Snippet for example

Document abc = Jsoup.connect("http://www.abc.in/").timeout(0).get();
Elements ee = abc.select("a[href*=xyz]");// all hrefs containing xyz substring 
String xyz = ee.first().attr("abs:href");

这篇关于如何获得一个“a href” jSoup在一个html类中使用了很多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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