分配 写一个程序去上网并获得股票的价格。 用法如下: getprice(股票代码) 例如:
getprice goog
getprice msft
40%
We will get stock quotes from Yahoo, from pages like these:
http://finance.yahoo.com/q?s=goog
http://finance.yahoo.com/q?s=msft
We use Yahoo because stock price is nicely delimited by tags. Something like:
<span id="yfs_l84_goog">529.24</span>
<span id="yfs_l84_msft">41.60</span>
The first line of your script constructs the URL needed, given the command-line argument.
Use wget to download the URL to a file.
Test that is working, and the stock price is found somewhere in the file.
When the above is working: Use grep to search the file for the relevant line.
Test that you can extract the single line with the stock price on it.
100%
When the above is working: Use sed to remove from start-of-line to <span>
See string matching / regular expressions.
See start of line, end of line.
".*" matches any sequence of characters.
Check that this works before proceeding.
When the above is working: Use sed to remove from </span> to end of line.
Your program should now just print the price.
这就是我所拥有的:
wget -q -O - http://finance.yahoo.com/q?s=$1 |
grep -o '<span id="yfs_l84_goog">.*</span>*' |
cut -c1-37 |
sed 's|<span id="yfs_l84_goog".||g' |
sed 's|</span>||g'
当我进入getprice goog它有效但是当我输入getprice msft时,有人不能向我解释我做错了什么?感谢
答案 0 :(得分:0)
100%:
wget -q -O - http://finance.yahoo.com/q?s=$1 |
grep -o "<span id=\"yfs_l84_$1\">.*</span>*" |
cut -c1-37 |
sed "s|<span id=\"yfs_l84_$1\".||g" |
sed 's|</span>||g'|
sed 's|</||g'
答案 1 :(得分:0)
function getprice () {
wget -q -O - http://finance.yahoo.com/q?s=$1 | sed -n "s/^.*yfs_l84_"$1"\">\([^<]*\)[<].*$/\1/p"
}
获得价格goog值:
getprice goog
#726.82
获取价格msft值:
getprice msft
#53.07