【问题标题】:Xidel: How to process multiple result from one node?Xidel:如何处理来自一个节点的多个结果?
【发布时间】:2020-06-16 05:24:36
【问题描述】:

说,我们有一个代码:

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将输出的每个值一个一个处理。

【问题讨论】:

  • 您不想要一个下载整个网页 8 次的解决方案,只是为了打印 8 个不同的节点。您需要一个下载网页一次 并选择 8 个不同节点的解决方案。您应该做的第一件事是摆脱for 循环。
  • 是的,我也是这么想的,但是在节点之后如何解决 [number] 的问题?
  • [position() &lt;= 8]怎么样
  • 这样吗? (//span[@class="number"]) [position() &lt;= 8]?我使用 for 循环的原因是处理每个输出值,而不仅仅是打印它。
  • @Tomalak 做到了,谢谢您的回复。帖子里已经有了自己的答案,最终我用forLoop来解决。

标签: bash for-loop xpath xidel


【解决方案1】:

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

【讨论】:

  • 感谢@Reino,您的精彩回答。虽然,xidel let 似乎不支持if else 状态,但它完全回答了我的帖子问题。
  • @AhiungLim 请详细说明。 if 语句不应该成为 let 表达式中的问题。
  • 让我们在这里使用以下代码: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,50,100),因为第 1 行和第 2 行已经有值为 1,所以不需要除。毕竟,我们需要像 1 & 2、3 & 4 等并排回显它,例如:905000 - 825000 853000 - 825000 ...
  • @AhiungLim 我知道你要去哪里了。如果你问我,这老实说值得一个新问题。但无论如何,这就是gist.github.com/Reino17/2f166849a0ebe8c64a36613723ebac64,你在找什么?顺便说一句,请在(提取)查询之前指定输入。
  • omg @Reino 你是 xidel 真正的专家,不是吗?非常感谢您的出色回答。
【解决方案2】:

示例代码:

$ 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 公式:

#"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

欢迎提出其他意见

【讨论】:

    猜你喜欢
    • 2011-11-12
    • 2020-10-14
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多