假设我有一个包含网页链接列表的文件。
www.xyz.com/asdd
www.wer.com/asdas
www.asdas.com/asd
www.asd.com/asdas
我知道做curl www.xyz.com/asdd
会获取该网页的html。我想从该网页获取一些数据。
所以场景是使用curl逐个命中文件中的所有链接,从网页中提取一些数据并存储在其他地方。任何想法或建议。
如评论中所示,这将循环通过your_file
和curl
每行:
while IFS= read -r line
do
curl "$line"
done < your_file
要获得页面的<title>
,你可以像这样:grep
:
grep -iPo '(?<=<title>).*(?=</title>)' file
所以你们可以一起做
while IFS= read -r line
do
curl -s "$line" | grep -Po '(?<=<title>).*(?=</title>)'
done < your_file
注意curl -s
用于静音模式。查看google page的示例:
$ curl -s http://www.google.com | grep -Po '(?<=<title>).*(?=</title>)'
302 Moved
您可以使用xargs
在一行中完成此操作。假设您在工作目录中有一个文件,其中包含所有URL(每行一个),名为sitemap
xargs -I{} curl -s {} <sitemap | grep title
这将提取任何带有“title”字样的行。要提取标题标签,您需要稍微更改grep
。 -o
标志确保只打印grepped结果:
xargs -I{} curl -s {} <sitemap | grep -o "<title>.*</title>"
\
转义字符。
例如,对于HTML属性,您应该匹配单引号和双引号,并像[\"\']
一样转义它们curl
输出。如果您检测到这一点,则需要使用iconv
等实用程序切换编码