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
758 views
in Technique[技术] by (71.8m points)

bash XHTML parsing using xpath

I'm writing a small script to learn how to parse an XHTML web page. The following command:

cat q?s=goog.xhtml | xpath '//span[@id="yfs_l10_goog"]'

returns:

Found 2 nodes:
-- NODE --
<span id="yfs_l10_goog">624.50</span>-- NODE --
<span id="yfs_l10_goog">624.50</span>

How do I:

  • need to write my command in order to only extract the value 624.50 ?

  • what do I need to do to extract it only once ?

source page I'm parsing: http://finance.yahoo.com/q?s=goog

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Edit 2:

Give this a try:

xpath -q -e '//span[@id="yfs_l10_goog"][1]/text()'

Edit:

Pipe your output through:

sed -n '/span/{s/<span[^<]*>([^<]*)<.*/1/;p;q}'

Original answer:

Using xmlstarlet:

echo -e '<foo><span id="yfs_l10_goog">624.50</span>
<bar>xyz</bar><span id="yfs_l10_goog">555.50</span>
<span id="yfs_l10_goog">123.50</span></foo>' | 
    xmlstarlet sel -t -v "//span[@id='yfs_l10_goog']"

Result of query:

624.50

Result of echo:

<foo><span id="yfs_l10_goog">624.50</span>
<bar>xyz</bar><span id="yfs_l10_goog">555.50</span>
<span id="yfs_l10_goog">123.50</span></foo>

Result of xml fo:

<?xml version="1.0"?>
<foo>
  <span id="yfs_l10_goog">624.50</span>
  <bar>xyz</bar>
  <span id="yfs_l10_goog">555.50</span>
  <span id="yfs_l10_goog">123.50</span>
</foo>

Other queries:

$ echo -e '...' | xmlstarlet sel -t -v "//span[@id='yfs_l10_goog'][1]"
624.50
$ echo -e '...' | xmlstarlet sel -t -v "//span[@id='yfs_l10_goog'][3]"
123.50
$ echo -e '...' | xmlstarlet sel -t -v "//span[@id='yfs_l10_goog'][last()]"
123.50

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

...