Getting the URLs for the first Google search results in a shell script

后端 未结 6 1995
北恋
北恋 2021-02-06 01:19

It\'s relatively easy to parse the output of the AJAX API using a scripting language:

#!/usr/bin/env python

import urllib
import json

base = \'http://ajax.goog         


        
6条回答
  •  时光取名叫无心
    2021-02-06 02:01

    Lri's answer only returned the last result for me and i needed the top so I changed it to:

    JSON=$(curl -s --get --data-urlencode "q=QUERY STRING HERE" http://ajax.googleapis.com/ajax/services/search/web?v=1.0 | python -mjson.tool)
    response=$(echo "$JSON" | sed -n -e 's/^.*responseStatus\": //p')
    if [ $response -eq 200 ] ; then 
        url=$(echo "$JSON" | egrep "unescapedUrl" | sed -e '1!d' -e "s/^.*unescapedUrl\": \"//" -e "s/\".*$//")
        echo "Success! [$url]"
        wget $url;
    else 
        echo "FAILED! [$response]" 
    fi
    

    Its not as compact as I'd like but in a rush.

提交回复
热议问题