Click here to Skip to main content
15,896,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello
i want to deal with google search api to return a list of url.

like i want to search for the word : mobile
so it will return to me a list of all the url would appear in google.com
just the url ... I didn't use google search api before

so any one can help me ?

note : i need it in c# language.
note 2 : i need all the url that appear in search.
Posted

XML
<script>
    (function () {
        var cx = '001111315940323651598:b5c5-62jb84';
        var gcse = document.createElement('script'); gcse.type = 'text/javascript';
        gcse.async = true;
        gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
            '//www.google.com/cse/cse.js?cx=' + cx;
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(gcse, s);
    })();
</script><pre lang="HTML">
<div style="width:350px;"><gcse:search></gcse:search></div>
 
Share this answer
 
action.....................

C#
[HttpPost]
        public ActionResult GetResult(string term)
        {
            string apiKey = "Your api key";
            string cx = "your search key";
            string query = term;

            WebClient webClient = new WebClient();
            webClient.Headers.Add("user-agent", "Only a test!");

            var results = webClient.DownloadString(String.Format("https://www.googleapis.com/customsearch/v1?key={0}&cx={1}&q={2}&alt=json", apiKey, cx, query));

           // web.Dispose();
            return Json(results);
        }


index.chhtml................................



C#
  <form id="form1">
        <table width="100%">
            <tr>
                <td colspan="2">
                    <input id="srchterm" type="text" style="width:500px" />
                    <button >Search</button>
                </td>
            </tr>
            <tr align="left">
                <td style="width:50%">
                    <div class="demo-container"></div>
                </td>
                <td align="center" style="width:50% !important;">
                    <div id="divResultPreview" class="divresultpreview">

                    </div>
                    <div id="divLoading" style="margin: 0px; padding: 0px; removed: fixed; removed 0px;<br mode=" hold=" />                    top: 0px; width: 100%; height: 100%; background-color: #333333; z-index: 30001;<br mode=">


<p style="removed: absolute; removed 40%; removed 40%; color: White;font-size:medium">
    Loading, please wait...<br /><img src="http://localhost:54539/Content/loading-image.gif">
</img></p>
                </div>
        </td>
    </tr>
</table>
</form>

JavaScript
<script type="text/javascript" language="javascript">
    function SearchWeb() {
        try {
            $("#divLoading").show();
            var url = '@Url.Action("GetResult", "Home")';
            var term = $("#srchterm").val();
            $.post(url, { term: term },
           function (data) {
               var obj = jQuery.parseJSON(data);
               var strHtml = "";
               if (obj.items.length > 0) {
                   for (var i = 0; i < obj.items.length; i++) {
                       strHtml = strHtml + "<table id="\"restable"" i="" style="\"border:0px\""><tr><td><fieldset style="\"border-style:none\"" ><a href="\""" style="\"font-size:medium;margin:2px;\"">" + obj.items[i].title + "</a><br />";
                       strHtml = strHtml + "<label style="\"color:Green;style=\"font-size:x-small;margin:2px;\"">" + obj.items[i].link + "</label><br />";
                       strHtml = strHtml + "<label style="\"color:Blue;margin:2px;\"">" + obj.items[i].htmlSnippet + "</label><br />";
                       strHtml = strHtml + "<label id="\"lblCacheUrl"" i="" style="\"display:none\"">" + obj.items[i].displayLink + "</label>";
                       strHtml = strHtml + "</fieldset></td><td>";
                       strHtml = strHtml + "<img id="\"showimg"" i="" src="\"http://cdn1.iconfinder.com/data/icons/30_Free_Black_ToolBar_Icons/40/Black_Circle-Arrow.png\"" style="\"filter:" ></img></td></tr></table>";
                   }
                   $('div.demo-container').html(strHtml);
                   $("#divLoading").hide();
               }
           });
        } catch (err) { }
    }</script>
 
Share this answer
 
v2
Comments
King Fisher 23-Feb-15 4:16am    
Why are you answering two months old Question?
The URL to search example string "hello world" is :
https://www.google.com.au/search?q=hello%20world[^]

Use this with HttpWebRequest and read the response. After that there will be some work to do to extract the information.
 
Share this answer
 
Comments
ENG.Samy Sammour 18-Dec-14 18:26pm    
its return a page result if i put it in a label so it will be google page but the thing i want is only the search url result

just a list of url
ENG.Samy Sammour 18-Dec-14 18:41pm    
i put the code result in a textbox and found manually the link but is there a way to find all the url written there in some function ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900