<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta name="generator" content="pdf2htmlEX">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet" href="https://static.pudn.com/base/css/base.min.css">
<link rel="stylesheet" href="https://static.pudn.com/base/css/fancy.min.css">
<link rel="stylesheet" href="https://static.pudn.com/prod/directory_preview_static/6267af2c4f8811599ef11d82/raw.css">
<script src="https://static.pudn.com/base/js/compatibility.min.js"></script>
<script src="https://static.pudn.com/base/js/pdf2htmlEX.min.js"></script>
<script>
try{
pdf2htmlEX.defaultViewer = new pdf2htmlEX.Viewer({});
}catch(e){}
</script>
<title></title>
</head>
<body>
<div id="sidebar" style="display: none">
<div id="outline">
</div>
</div>
<div id="pf1" class="pf w0 h0" data-page-no="1"><div class="pc pc1 w0 h0"><img class="bi x0 y0 w1 h1" alt="" src="https://static.pudn.com/prod/directory_preview_static/6267af2c4f8811599ef11d82/bg1.jpg"><div class="c x0 y1 w2 h2"><div class="t m0 x1 h3 y2 ff1 fs0 fc0 sc0 ls0 ws0">Google Command Line Scrip<span class="_ _0"></span>t</div><div class="t m0 x1 h4 y3 ff1 fs1 fc0 sc0 ls0 ws0">To make it a bit interactive, we will ask the user for an inpu<span class="_ _1"></span>t and save the result to a</div><div class="t m0 x1 h4 y4 ff1 fs1 fc0 sc0 ls0 ws0">variable that we name “query”<span class="_ _1"></span>.</div><div class="t m0 x1 h5 y5 ff2 fs1 fc0 sc0 ls0 ws0">query = raw_input("What do you want to search for ? >> ")</div><div class="t m0 x1 h4 y6 ff1 fs1 fc0 sc0 ls0 ws0">Create the response object by loading the the URL re<span class="_ _1"></span>sponse, including the query </div><div class="t m0 x1 h4 y7 ff1 fs1 fc0 sc0 ls0 ws0">we asked for above.</div><div class="t m0 x1 h5 y8 ff2 fs1 fc0 sc0 ls0 ws0">response = urllib2.urlopen (url + query ).read()</div><div class="t m0 x1 h4 y9 ff1 fs1 fc0 sc0 ls0 ws0"># Process the JSON string. da<span class="_ _1"></span>ta = json.loads (response)</div><div class="t m0 x1 h4 ya ff1 fs1 fc0 sc0 ls0 ws0">From this point we can play around with th<span class="_ _1"></span>e results</div><div class="t m0 x1 h6 yb ff3 fs1 fc0 sc0 ls0 ws0">GoogleSearch.py</div><div class="t m0 x1 h4 yc ff1 fs1 fc0 sc0 ls0 ws0">Let’s see the complete script</div><div class="t m0 x1 h5 yd ff2 fs1 fc0 sc0 ls0 ws0">import urllib2</div><div class="t m0 x1 h5 ye ff2 fs1 fc0 sc0 ls0 ws0">import urllib</div><div class="t m0 x1 h5 yf ff2 fs1 fc0 sc0 ls0 ws0">import json</div><div class="t m0 x1 h5 y10 ff2 fs1 fc0 sc0 ls0 ws0">url = </div><div class="t m0 x1 h5 y11 ff2 fs1 fc0 sc0 ls0 ws0">"http://ajax.googleapis.com/ajax/services/search/web?</div><div class="t m0 x1 h5 y12 ff2 fs1 fc0 sc0 ls0 ws0">v=1.0&"</div><div class="t m0 x1 h5 y13 ff2 fs1 fc0 sc0 ls0 ws0">query = raw_input("What do you want to search for ? >> ")</div><div class="t m0 x1 h5 y14 ff2 fs1 fc0 sc0 ls0 ws0">query = urllib.urlencode( {'q' : query } )</div><div class="t m0 x1 h5 y15 ff2 fs1 fc0 sc0 ls0 ws0">response = urllib2.urlopen (url + query ).read()</div><div class="t m0 x1 h5 y16 ff2 fs1 fc0 sc0 ls0 ws0">data = json.loads ( response )</div><div class="t m0 x1 h5 y17 ff2 fs1 fc0 sc0 ls0 ws0">results = data [ 'responseData' ] [ 'results' ]</div><div class="t m0 x1 h5 y18 ff2 fs1 fc0 sc0 ls0 ws0">for result in results:</div><div class="t m0 x1 h5 y19 ff2 fs1 fc0 sc0 ls0 ws0"> title = result['title']</div><div class="t m0 x1 h5 y1a ff2 fs1 fc0 sc0 ls0 ws0"> url = result['url']</div><div class="t m0 x1 h5 y1b ff2 fs1 fc0 sc0 ls0 ws0"> print ( title + '; ' + url )</div><div class="t m0 x1 h4 y1c ff1 fs1 fc0 sc0 ls0 ws0">Open an text editor , copy & paste the code above.</div></div></div><div class="pi" data-data='{"ctm":[1.611850,0.000000,0.000000,1.611850,0.000000,0.000000]}'></div></div>
</body>
</html>