Xidel:如何处理来自一个节点的多个结果?

Xidel: How to process multiple result from one node?

说,我们有一个代码:

xidel -s https://www.example.com -e '(//span[@class="number"])'

输出为:

111111
222222
333333

我可以在下面做这个吗?

for ((n=1;n<=3;n++))
do
   a=$(xidel -s https://www.example.com -e '(//span[@class="number"]) [$n]')
   b=$a+1
   echo $b
done

我希望它能像这样打印出 3 个经过编辑的数字:

111112
222223
333334

下载网页3次可能有点荒谬,但这里的原因是使用ForLoop将输出的每个值一个一个地处理。

示例代码:

$ aa=$(xidel -se '//span[@class="random"]' 'https://www.example.com')
$ echo $aa

假设 xidel 的结果如下:

a abc
a sdf
a wef
a vda
a gdr

and...假设我们想在这种情况下从这个列表的每个单词中删除所有 a,不限于仅排除 a.

我们可以这样使用For Loop公式:

#"a " is the one we want to remove, so make variable for this prefix
a="a "

for ((n=-1;n>=-5;n--))
do
 #process the extraction by selecting which line first
   bb=$(echo "$aa" | head $n | tail -1)
 #then remove the prefix after that
   bb=${aa/#$a}
   echo $bb

done

这将打印:

abc
sdf
wef
vda
gdr

奖金

#"a " is the one we want to remove, so make variable for this prefix
a="a "

for ((n=-1;n>=-5;n--))
do
 #process the extraction by selecting which line first
   bb=$(echo "$aa" | head $n | tail -1)
 #then remove the prefix after that
   bb=${aa/#$a}
 #echo everything except 2nd line
 if [ $n != -2 ] ; then
 echo $bb
 fi

done

这将打印:

abc
wef
vda
gdr

欢迎任何其他意见

xidel 完全支持 XPath/XQuery 3.0(对 XPath/XQuery 3.1 的支持正在开发中),因此您可以使用它提供的所有功能和过滤器。
我可以推荐以下网站:


如果没有“Minimal, Reproducible Example”,我只会将您上面提到的输出按顺序排列并向您展示一些示例。

xidel -se 'let $a:=(111111,222222,333333) return $a ! (. + 1)'
#or
xidel -se 'for $x in (111111,222222,333333) return $x + 1'
111112
222223
333334
xidel -se 'let $a:=("a abc","a sdf","a wef","a vda","a gdr") return $a ! substring-after(.,"a ")'
#or
xidel -se 'let $a:=("a abc","a sdf","a wef","a vda","a gdr") return $a ! replace(.,"a ","")'
#or
xidel -se 'for $x in ("a abc","a sdf","a wef","a vda","a gdr") return substring-after($x,"a ")'
#or
xidel -se 'for $x in ("a abc","a sdf","a wef","a vda","a gdr") return replace($x,"a ","")'
abc
sdf
wef
vda
gdr