【问题标题】:Shell script using curl to loop through urls使用 curl 循环访问 url 的 Shell 脚本
【发布时间】:2013-04-14 10:09:05
【问题描述】:

我一直在尝试创建一个简单的脚本,该脚本将从 .txt 文件中获取查询列表,附加主 url 变量,然后抓取内容并将其输出到文本文件。

这是我目前所拥有的:

#!/bin/bash

url="example.com/?q="
for i in $(cat query.txt); do
    content=$(curl -o $url $i)
    echo $url $i
    echo $content >> output.txt
done

列表:

images
news
stuff
other

错误日志:

curl: (6) Could not resolve host: other; nodename nor servname provided, or not known
example.com/?q= other

如果我直接从命令行使用这个命令,我会在文件中得到一些输出:

curl -L http://example.com/?q=other >> output.txt

最终我希望输出是:

fetched:    http://example.com/?q=other
content:    the output of the page

followed by the next query in the list.

【问题讨论】:

    标签: bash loops curl


    【解决方案1】:

    使用更多引号!

    试试这个:

    url="example.com/?q="
    for i in $(cat query.txt); do
        content="$(curl -s "$url/$i")"
        echo "$content" >> output.txt
    done
    

    【讨论】:

    • 我想这应该是非常明显的。 :p 非常感谢! :)
    【解决方案2】:

    你有嵌套的引号,试试这样:

    #!/bin/bash
    
    url=https://www.google.fr/?q=
    while read query
    do
        content=$(curl "{$url}${query}")
        echo $query
        echo $content >> output.txt
    done < query.txt
    

    【讨论】:

      猜你喜欢
      • 2017-11-13
      • 1970-01-01
      • 1970-01-01
      • 2017-06-14
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多