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

Bash Xidel:如何处理来自一个节点的多个结果?,bash,for-loop,xpath,xidel,Bash,For Loop,Xpath,Xidel,比如说,我们有一个代码: 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 下载网页

比如说,我们有一个代码:

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次可能有点混乱,但这里的原因是使用ForLoop逐个处理输出的每个值

示例代码:

$ aa=$(xidel -se '//span[@class="random"]' 'https://www.example.com')
$ echo $aa
假设xidel的结果如下:

a abc
a sdf
a wef
a vda
a gdr
并且……假设我们想在这种情况下从列表中的每个单词中删除所有
a
,而不仅仅是排除
a

我们可以使用
For Loop
公式如下:

111112
222223
333334
#"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
abc
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
sdf
wef
vda
gdr
abc
wef
vda
gdr

欢迎任何其他输入

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

  • ,

如果没有“”的话,我将把上面提到的输出按顺序排列,并向您展示一些示例

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

您不希望一个解决方案只需将整个网页下载8次就可以打印8个不同的节点。您需要一个一次性下载网页并选择8个不同节点的解决方案。你应该做的第一件事是摆脱
for
循环。是的,我也是这么想的,但是在节点之后如何处理[number]的事情呢?像这样
[position()怎么样?
(//span[@class=“number”])[position())@Tomalak做到了,谢谢你的回复。我在帖子中已经有了我自己的答案,最终我用forLoop来解决。谢谢@Reino,谢谢你的精彩回答。虽然,似乎xidel
let
不支持
if else
statment,但它完全回答了我的帖子问题。@AhiungLim请详细说明。如果声明不应该e let-expression中有一个问题。让我们在这里使用下面的代码:
xidel-se'//div[@class=“product box”]//span[@class=“n-heading”]''https://www.anekalogam.co.id/id“
它应该以卢比打印16个价格,因此我们应该首先删除“Rp”和“.”,然后对于第3行和第4行,值被2除,对于第5行和第6行,值被3除,依此顺序(1,2,3,5,10,25,50100),因为第1行和第2行的值已经是1,所以它不需要被分割。毕竟,我们需要像1和2、3和4这样并排重复它。例如:
905000-825000-853000-825000…
@AhiungLim我知道你要去哪里。如果你问我,这确实值得一个新问题。但不管怎样,这是你想要的吗顺便说一句,请在(提取-)查询之前指定输入。omg@Reino你真的是xidel方面的专家,不是吗?我衷心感谢你的出色回答。