0
#!/bin/sh

URL1=http://runescape.com/title.ws
tot=`wget  -qO- $URL1  | grep -i PlayerCount | cut -d\> -f4 | cut -d\< -f1 | sed -e's/,//'`
URL2=http://oldschool.runescape.com
b=`wget -qO- $URL2| grep "people playing" | awk '{print $4}'`
a=`expr $tot - $b`
export LC_ALL=en_US.UTF-8
a_with_comma=`echo $a | awk "{printf \"%'d\n\", \\$1}"`
echo "$a_with_comma people `date '+%r %b %d %Y'`"

这从 URL1 和 URL2 中获取 2 个数字,并从 URL2 中减去 URL1。尝试从http://www.runescape.com/title.ws (URL1)获取“48,877 玩家在线”

URL2 工作正常我只是无法获得 URL1。

4

2 回答 2

1

你可以换...

tot=`wget  -qO- $URL1  | grep -i PlayerCount | cut -d\> -f4 | cut -d\< -f1 | sed -e's/,//'`

... 到 ...

 tot=`wget  -qO- $URL1 | grep -i playercount | cut -d\> -f5 | cut -d\< -f1 | sed -e's/,//'`

……如果你赶时间的话。否则,您可能想听从 Tripleee 的建议。谁知道您可能会从 ACM 获得奖项 :-)

于 2013-09-02T04:23:27.833 回答
1

这是将原始文件重构为两个实例的快速尝试awk,以消除大部分扭曲。

#!/bin/sh

#URL1=http://runescape.com/title.ws
#tot=$(wget -qO- "$URL1" | awk 'tolower($0) ~ /playercount/ {
#    # Trim anything after this expression
#    gsub(/<\/span> Players Online Now<\/span>.*/, "")
#    # From the remainder, trim anything up through last tag close
#    gsub(/.*>/, "")
#    # Should be left with a number.  Remove any thousands separator
#    gsub(/,/, "")
#    # Should have a computer-readable number now.  Print it
#    print }')
URL0='http://www.runescape.com/c=eWHvvLATbvs/player_count.js?varname=iPlayerCount&callback=jQuery17201610493347980082_1378103074657&_=1378103197632'
tot=$(wget -qO- "$URL0" | awk -F '[()]' '{ print $2 }'

URL2=http://oldschool.runescape.com
wget -qO- "$URL2" | awk -v tot=$tot -v fmt="%'d people " '
    /people playing/ { printf(fmt, tot-$4 )}'
date '+%r %b %d %Y'

的处理URL1现在应该更健壮一些,因为它会寻找 aspan后跟Players Online Now. 当然,他们可以随时更改页面的格式,从而再次中断。因此,如果他们提供 JSON API,使用 JSON API 可能会更好。(简短的谷歌搜索表明这存在,但没有记录。主要文档似乎在http://services.runescape.com/m=rswiki/en/Grand_Exchange_APIs但这与摘要播放器统计信息无关。)

当然,这些评论并不是绝对必要的。如果页面的源再次更改,它们应该可以帮助您找出要更改的内容,因此修剪它们不是一个好主意,除非您对 Awk 的了解足够好以至于不需要它们。

编辑:更新为使用 JSON API 来计算玩家总数——这应该更健壮,也更简单。为了以防万一,我将原始代码注释掉了。

于 2013-09-02T06:20:44.790 回答