echo "<table>" > todo.html
for c in 0 1 2 3 
do
for d in 0 1 2 3 4 5 6 7 8 9 
do

for u in 0 1 2 3 4 5 6 7 8 9 
do

	cod=${c}${d}${u}

	echo $cod
	rm -f ficha-${cod}
	rm -f correo-${cod}
	rm -f ficha$cod
	wget http://www.senado.es/legis8/senadores/ficha/${cod}.html -O ficha-${cod} -o pp
	wget http://www.senado.es/legis8/senadores/ficha/${cod}_correo.html -O correo-${cod} -o pp

	nombre=`cat ficha-$cod | grep TITLE | cut -d">" -f2 | cut -d"<" -f1`
	donde=`cat ficha-$cod | grep Senad | grep por | cut -d">" -f3 | cut -d"<" -f1`
	partido=`cat ficha-$cod | grep Logo | head -1 |  cut -d"\"" -f4 | cut -d"\"" -f1`
	echo "$partido"
	case $partido in 
		Log*)
			echo "lo cambio"
			partido=`cat ficha-$cod | grep Logo | head -1 |  cut -d"\"" -f2 | cut -d"\"" -f1`
		;;
		*) 
			echo "lo dejo como esta .."
	esac
	foto=`cat ficha-$cod | grep Foto |  cut -d"\"" -f2 | cut -d"<" -f1`
	correo=`cat correo-$cod | grep mailto | cut -d"\"" -f2 | cut -d":" -f2`

	case $nombre in 
		"")
			;;
		*)
			echo "$nombre|$correo|$donde|$partido|$foto" > ficha$cod

			echo "<tr>" >> todo.html
			echo "<td>
			<a href='http://www.senado.es/legis8/senadores/${cod}_index.html'><img width=80 src='http://www.senado.es/$foto'>
			</a></td>" >> todo.html
			echo "<td>
			<a href='http://www.senado.es/legis8/senadores/${cod}_index.html'>
			<b>$nombre</b>
			</a></td>" >> todo.html
			echo "<td>$donde</td>" >> todo.html
			echo "<td><img src='http://www.senado.es/$partido'></td>" >> todo.html
			echo "<td><a href='mailto:$correo'>$correo</a></td>" >> todo.html
			echo "</tr>" >> todo.html
			;;
	esac
done 
done 
done
echo "</table>" >> todo.html
