![为什么我的脚本不起作用?](https://linux22.com/image/155878/%E4%B8%BA%E4%BB%80%E4%B9%88%E6%88%91%E7%9A%84%E8%84%9A%E6%9C%AC%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8%EF%BC%9F.png)
我编写这个脚本是为了一些网络抓取,但它不起作用。
我是初学者,所以我不知道,也因为它没有显示错误。
我跟着这文章。
#!/bin/bash
n="1"
rm -f tmp_file extractData.csv
# colunas do arquivo CSV
echo "nome;local;acervo" > livrariasevsp.csv
while [ $n -lt 10 ]
do
# exec o curl e salva no arquivo temporario
curl "https://www.estantevirtual.com.br/garimpepor/sebos-e-livreiros/sp?livraria=$n" > tmp_file
# contador "nome"
n=$[$n+1]
# get nome
nome=$(cat tmp_file |grep "class=\"sebo-nome\"" | cut -d'>' -f4 | cut -d'<' -f1)
# get local
local=$(cat tmp_file |grep "class=\"js-sebo-local\"" | grep span | cut -d'>' -f4 | cut -d'<' -f1)
# get acervo
acervo=$(cat tmp_file |grep "class=\"js-sebo-acervo\"" | grep span | cut -d'>' -f4 | cut -d'<' -f1)
# escreve os dados das livrarias no arquivo CSV
echo "$nome;$local;$acervo" >> livrariasevsp.csv
done
答案1
我通常不提倡使用正则表达式来解析 HTML。然而
awk -F'[<>]' '
BEGIN {print "nome;local;acervo"}
$2 ~ /^h2 .*-titulo/ {title = $3}
$2 ~ /^span .*-local/ {city = $3}
$2 ~ /^span .* nlivros=/ {printf "\"%s\";\"%s\";%d\n", title, city, $3}
' tmp_file > livrariasevsp.csv
请注意,$[...]
算术展开的形式没有记录。代替使用$((...))
。但是,您实际上不需要这样做。尝试这个:
{
echo "nome;local;acervo"
for n in {1..9}; do
url="https://www.estantevirtual.com.br/garimpepor/sebos-e-livreiros/sp?livraria=$n"
awk -F'[<>]' '
$2 ~ /^h2 .*-titulo/ {title = $3}
$2 ~ /^span .*-local/ {city = $3}
$2 ~ /^span .* nlivros=/ {
printf "\"%s\";\"%s\";%d\n", title, city, $3
}
' <( curl --silent "$url" )
done
} > livrariasevsp.csv