Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

iphone - Extracting an href's text from an html document

I'm trying to parse this piece of HTML:

<div>
  <p>
    <a href="#" class="transcriptLink" onclick="seekVideo(0); return false;">A few years ago,</a>
    <a href="#" class="transcriptLink" onclick="seekVideo(2000); return false;">I felt like I was stuck in a rut,</a>
    <a href="#" class="transcriptLink" onclick="seekVideo(5000); return false;">so I decided to follow in the footsteps</a>
    <a href="#" class="transcriptLink" onclick="seekVideo(7000); return false;">of the great American philosopher, Morgan Spurlock,</a>
    <a href="#" class="transcriptLink" onclick="seekVideo(10000); return false;">and try something new for 30 days.</a>
  </p>
</div>

I want to know how to get the text in label, such as: "A few years ago,"

I can get text in "<a> text </a>",

But I do not know how to get "A few years ago," in the label of "<a href="#" class="transcriptLink" onclick="seekVideo(0); return false;">A few years ago,</a> "

<a href="#" class="transcriptLink" onclick="seekVideo(0); return false;">  
<a href="#" class="transcriptLink" onclick="seekVideo(2000); return false;">
....................

There are different about only onclick="seekVideo(....);

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use XPath: /div/p/a[1]/text() - selects a by index or matching @onclick value: /div/p/a[starts-with(@onclick, 'seekVideo(0)')]/text(). So both queries return A few years ago,.

To get number in @onclick seekVideo you can use this expression:

substring-before(substring-after(@onclick, '('), ')')

e.g.: To find a whose @onclick seekVideo = 0 you can use this XPath:

/div/p/a[substring-before(substring-after(@onclick, '('), ')') = '0']/text()

or

/div/p/a[number(substring-before(substring-after(@onclick, '('), ')')) = 0]/text()

So both queries return A few years ago,.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...