我想从以下html输出中拉出HREF
<div class="course">
<p class="course-data">
<a class="course-name i13n-sec-country i13n-ltxt-meeting i13n-tab-TODO-TAG-TAB" href="/course?courseId=1.2&date=20150713">
Ayr
</a> | SOFT
</p>
<ul>
<li class="full" title="7f 50yds Maiden Stakes | 10 runners">
<a class="i13n-sec-country i13n-ltxt-race i13n-tab-TODO-TAG-TAB" href="/raceresult?raceId=1.2.1150713.1">
09:00
</a>
</li>
<li class="full" title="1m Handicap | 5 runners">
<a class="i13n-sec-country i13n-ltxt-race i13n-tab-TODO-TAG-TAB" href="/raceresult?raceId=1.2.1150713.2">
09:30
</a>
</li>
<li class="full" title="1m2f Handicap | 6 runners">
<a class="i13n-sec-country i13n-ltxt-race i13n-tab-TODO-TAG-TAB" href="/raceresult?raceId=1.2.1150713.3">
10:00
</a>
</li>
但是如果调用包含单词“race”,我只想要href属性 - 例如它会拉动这个href
<a class="i13n-sec-country i13n-ltxt-race i13n-tab-TODO-TAG-TAB" href="/raceresult?raceId=1.2.1150713.1">
我已经尝试了自己,但似乎无法使用getAttribute('href')
拉出href并获得错误“调用非对象上的成员函数项()”
答案 0 :(得分:3)
我认为这就是你要找的东西
try {
Scanner in = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = in.nextLine(); // <---- HERE
System.out.printf("\nEnter first integer: ");
int nnum1=Integer.parseInt(in.nextLine());
System.out.printf("\nEnter second integer: ");
int nnum2=Integer.parseInt(in.nextLine());
System.out.println("Hello \n" + name);
System.out.println("num 1" + "+" + "is " + (nnum1 + nnum2));
System.out.println("num 1" + "-" + "is " + (nnum1 - nnum2));
System.out.println("num 1" + "*" + "is " + (nnum1 * nnum2));
System.out.println("num 1" + "%" + "is " + (nnum1 % nnum2));
System.out.print("Thank You!");
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
System.out.println("Please enter valid number");
e.printStackTrace();
}
或者您可以使用SimpleXML
$dom = new DomDocument;
$dom->loadHtml($html);
$tagList = $dom->getElementsByTagName('a');
foreach ($tagList as $tag) {
// Simple string check, but beware using strstr and utf-8
if(strstr($tag->getAttribute('class'), 'race')) {
echo $tag->getAttribute('href');
}
}