Shell - 将html读入变量和过滤器序列

问题描述 投票:0回答:1

我需要将包含表格的网页读入变量并过滤掉一个单元格的数量。

HTML就像:

<tr><th>Totals:</th><td> 99999.9</td>

我需要得到99999.9号码。

我试过了:

value=$(curl -s -m 10 http://$host |  egrep -o "Totals:</th><td> [0-9]\{5\}" | cut -d'> ' -f 2)

另一个有效选项是检查页面是否至少生成。我的意思是将html读入一个值并检查该值是否充满html(可能是长度)。

任何胶水curl命令与cut命令结合的错误是什么?

谢谢?

html shell curl
1个回答
0
投票

您应该使用适当的html解析器。如果你真的想用bash(这很容易出错并且如果html变得越来越复杂会引起很多麻烦)你可以通过以下方式做到这一点:

# html="$(curl -s -m 10 http://$host)"
html="<tr><th>Totals:</th><td> 99999.9</td>"

# remove all whitespaces
# it is not guaranteed that your cell value will be on the same line with Totals:
html_cl="$(echo $html | tr -d ' \t\n\r\f')"

# strip .*Totals:</th><td> before the desired cell value
# strip </td>.* after the value
value="${html_cl##*Totals:</th><td>}"
value="${value%%</td>*}"
echo $value

给你结果:

99999.9

注意:如果您有多个具有相同标签的Totals,那么它将仅从您的字符串中提取最后一个。

© www.soinside.com 2019 - 2024. All rights reserved.