【问题标题】:How to create a curl loop to make my code more compact?如何创建 curl 循环以使我的代码更紧凑?
【发布时间】:2022-01-15 00:37:41
【问题描述】:

我打算做一个小的网络爬虫脚本:

我有一个 shell 脚本 scrape.sh,我使用 chmod 755 使其可执行

curl 用于抓取数据(我打算抓取 30,000 个 url)

scrape.sh 的内容:

        curl https://example.com/something/UID1 --output UID1.html
        curl https://example.com/something/UID2 --output UID2.html
        curl https://example.com/something/UID3 --output UID3.html
        curl https://example.com/something/UID4 --output UID4.html
    ...
        curl https://example.com/something/UID30000 --output UID30000.html

除了在我的 scrape.sh 中使用 30.000 行代码之外,还有什么更紧凑的方式来完成这项工作?

【问题讨论】:

  • 请问您是从哪里获得这些 UID 的?它们在文件中吗?它们可以通过编程方式生成吗?
  • curl 不是刮板。它只是下载网站的 html 源代码。因此,在您的 'scrape.sh' 中,您忘记包含用于提取所需信息的实际 HTML 解析器。

标签: shell curl


【解决方案1】:

在shell中有多种方法可以生成序列号。

您可以将seq 命令与 for 循环一起使用:

for id in $(seq 1 100); do
  echo "id is ${id}"
done

在许多现代 shell(如 bash、zsh、...)中,您可以更惯用地使用 for 循环:

for (( id = 0; id < 100; id++ )); do
  echo "id is ${id}"
done

在这些 shell 中,您还可以使用大括号扩展。

for id in {1..100}; do
  echo "id is $id"
done

附带说明一下,大括号扩展比这更酷 - 你可以使用前缀 (echo abc{1..5}),你可以使用字母序列 (echo {a..z}),甚至是排列 (echo {1..3}{a..c}{1..3})。

【讨论】:

    猜你喜欢
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 2011-04-17
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多