从网页中查找特定日期?

发布于 2025-01-10 08:49:32 字数 1468 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬色的秋千 2025-01-17 08:49:32

当前代码存在一些问题:

  • 默认情况下,awk 需要一个文件作为输入,但 modHTML 是一个(字符串)变量;要让 awk 处理变量,您可以使用此处字符串来模拟将字符串作为文件提供给 awk,例如: awk '/Update: /{ p=1}p' <<< "$modHTML"
  • modHTML="xmllint --nowarning ..." 正在将字符串 xmllint --nowarning ... 分配给 modHTML< /code> 当您真正想要的是运行 xmllint 调用并将结果存储在 modHTML 变量中时,例如 modHTML=$(xmllint --nowarning ...)

将这些更改滚动到OP的当前代码:

for mod in "${activeModList[@]}"
do
    modDirectory="modHTML/$mod"

    modHTML=$(xmllint --nowarning --html --xpath "/html/body/div[1]/div[7]/div[4]/div[1]/div[4]/div[11]/div[1]/div[2]/div[1]" "$modDirectory.html")

    lastUpdateTime=$(awk '/Update: /{p=1}p' <<< "$modHTML")

    # uncomment following line to assist with debugging; this will
    # show you exactly what's stored in the variables thus allowing
    # you to verify if your code is doing what you think it's doing

    # typeset -p modHTML lastUpdateTime

    echo "$mod last updated: $lastUpdateTime"
done

注释:

  • 我不使用xmllint,因此我无法评论这是否是有效的调用,但至少建议的代码更改应该允许OP为了更接近期望的结果,
  • 可以调整awk调用以提供更紧凑的答案,但我将把它留给OP来处理(一旦我们通过了语法错误并开始生成实际输出)

Couple issues with the current code:

  • by default awk expects a file as input but modHTML is a (string) variable; to have awk process a variable you can use a here-string to simulate feeding the string as a file to awk, eg: awk '/Update: /{p=1}p' <<< "$modHTML"
  • modHTML="xmllint --nowarning ..." is assigning the string xmllint --nowarning ... to modHTML when what you really want is to run the xmllint call and store the results in the modHTML variable, eg, modHTML=$(xmllint --nowarning ...)

Rolling these changes into OP's current code:

for mod in "${activeModList[@]}"
do
    modDirectory="modHTML/$mod"

    modHTML=$(xmllint --nowarning --html --xpath "/html/body/div[1]/div[7]/div[4]/div[1]/div[4]/div[11]/div[1]/div[2]/div[1]" "$modDirectory.html")

    lastUpdateTime=$(awk '/Update: /{p=1}p' <<< "$modHTML")

    # uncomment following line to assist with debugging; this will
    # show you exactly what's stored in the variables thus allowing
    # you to verify if your code is doing what you think it's doing

    # typeset -p modHTML lastUpdateTime

    echo "$mod last updated: $lastUpdateTime"
done

NOTES:

  • I don't use xmllint so I can't comment on whether or not this is a valid call but at least the proposed code changes should allow the OP to get a bit closer to the desired result
  • the awk call can probably be tweaked to provide a more compact answer but I'll leave that up to the OP to work on (once we get past the syntax errors and start generating actual output)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文