1

我得到了http://www.cpubenchmark.net/cpu_list.php页面,我想提取给定 CPU 的名称、排名和基准分数。

示例(“英特尔酷睿 i5”):

Intel Core i5-3450 @ 3.10GHz - Score: 3333 - Rank: 1
Intel Core i5-3450S @ 2.80GHz - Score: 2222 - Rank: 2
Intel Core i5-2380P @ 3.10GHz - Score: 1111 - Rank: 3
...

我怎么能在 bash 中做到这一点?试图从类似的东西开始(没有 cpu 过滤 - 不知道它是如何工作的):

#!/bin/sh
curl http://www.cpubenchmark.net/cpu_list.php | grep '^<TR><TD>' \
| sed \
    -e 's:<TR>::g'  \
    -e 's:</TR>::g' \
    -e 's:</TD>::g' \
    -e 's:<TD>: :g' \
| cut -c2- >> /home/test.txt

输出是这样的:

<A HREF="cpu_lookup.php?cpu=686+Gen&amp;id=1495">686 Gen</A> 288 1559 NA NA
<A HREF="cpu_lookup.php?cpu=AMD+A10-4600M+APU&amp;id=10">AMD A10-4600M APU</A> 3175 388 NA NA
<A HREF="cpu_lookup.php?cpu=AMD+A10-4655M+APU&amp;id=11">AMD A10-4655M APU</A> 3017 406 NA NA
4

2 回答 2

4

如果你想下载一个额外的程序,你可以使用我的Xidel

所有 CPU:

xidel http://www.cpubenchmark.net/cpu_list.php -e '//table[@id="cputable"]//tr/concat(td[1], " - Score: ", td[2], " - Rank: ", td[3])'

那些以英特尔开头的...:

xidel http://www.cpubenchmark.net/cpu_list.php -e '//table[@id="cputable"]//tr[starts-with(td[1], "Intel Core i5")]/concat(td[1], " - Score: ", td[2], " - Rank: ", td[3])'

它甚至可以对它们进行排名(以前从未使用过该功能):

xidel http://www.cpubenchmark.net/cpu_list.php -e 'for $row in //table[@id="cputable"]//tr[starts-with(td[1], "Intel Core i5")] order by $row/td[3] return $row/concat(td[1], " - Score: ", td[2], " - Rank: ", td[3])' --extract-kind=xquery
于 2012-12-29T00:13:59.423 回答
0

严格针对当前页面格式量身定制的 bash 解决方案:

#! /bin/bash

function nextcell
{
    cell=${line%%</TD>*}
    # remove closing link tag if any
    cell=${cell%</?>}
    cell=${cell##*>}
    line=${line#*</TD>}
}

while read line
do
    if [[ ! "$line" =~ cpu_lookup.php ]]
    then
        continue
    fi
    nextcell
    echo -n "$cell"
    nextcell
    echo -n " - Score: $cell"
    nextcell
    echo " - Rank: $cell"
done
于 2012-12-29T00:38:56.610 回答