我首先需要html
在野外运行类似的东西吗jsoup
?不让它在人类意义上有效,可能会把它变成乱码,但至少可以xmlstarlet
处理文件?
最好寻找一个可以像这样安装和使用的 CLI:
massage foo.html > bar.xhtml
或者至少是沿着这些思路的东西。
使用案例:
thufir@doge:~/.html$
thufir@doge:~/.html$ curl http://int.soccerway.com/ > soccer.html
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 188k 0 188k 0 0 313k 0 --:--:-- --:--:-- --:--:-- 313k
thufir@doge:~/.html$
thufir@doge:~/.html$ xmlstarlet sel -t -v "/html/body/table/tr/td[1]" -n soccer.html
soccer.html:70.13: xmlParseEntityRef: no name
if (this.$ && this.$.fn && this.$.fn.jquery) {
^
soccer.html:70.14: xmlParseEntityRef: no name
if (this.$ && this.$.fn && this.$.fn.jquery) {
^
soccer.html:70.26: xmlParseEntityRef: no name
if (this.$ && this.$.fn && this.$.fn.jquery) {
^
soccer.html:70.27: xmlParseEntityRef: no name
if (this.$ && this.$.fn && this.$.fn.jquery) {
^
soccer.html:198.8: Opening and ending tag mismatch: link line 27 and head
</head>
^
soccer.html:209.45: EntityRef: expecting ';'
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
^
soccer.html:223.40: xmlParseEntityRef: no name
if (typeof(e.data) === 'string' && (e.data.indexOf('onEplayerVideoStarted'
^
理想情况下会htmlstarlet
直接针对 URL 运行,但似乎没有这样的规定。
那里是一个fo
选项格式,但我无法得到与上面不同的结果。
答案1
如果您只需要表格的数据单元格,可以使用xmlstarlet fo
后跟xmlstarlet sel
。您遇到的主要问题是 XPath。如果添加几个“通配符”元素 ( //
),您将获得所需的结果:
# fetch URL silently, following redirects, and send to standard out
curl -sL http://int.soccerway.com/ |
# interpret input as HTML (-H) and try to recover as much as possible (-R)
xmlstarlet fo -H -R 2> /dev/null |
# use the following XPath expression and return the value (-t -v),
# also add a newline after the result (-n)
xmlstarlet sel -t -v '//table//tr//h3/span' -n 2> /dev/null |
# only show the first 10 values
head -n10
输出:
World - Friendlies
Argentina - Prim B Nacional
Australia - National Premier Leagues
Australia - NPL Youth League
Bangladesh - Premier League
Belarus - Premier League
Benin - Championnat National
Brazil - Serie A
Brazil - Serie D
Brazil - Copa Paulista