Click here to Skip to main content
15,891,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I need to do a google search for some specific keywords and provide the links that are outputted from the search

C#
string keywordString = Title;
            string uriString = " https://www.google.com/search?q=" + keywordString;
            WebClient webClient = new WebClient();

            NameValueCollection nameValueCollection = new NameValueCollection();
            nameValueCollection.Add("q", keywordString);

            webClient.QueryString.Add(nameValueCollection);
            Console.WriteLine(webClient.DownloadString(uriString.ToString()));
            string LinksHTML = webClient.DownloadString(uriString.ToString());

            // ExtractingLinks
            //string text = System.IO.File.ReadAllText(@"F:\fixable q mark\fixable q mark\die.txt");
            if (LinksHTML.Contains("<a href=")&& LinksHTML.Contains("ping="))
            {
                Console.WriteLine("lol");
                var matches = Regex.Matches(LinksHTML, "<a href=(.*?) ping=");
                //var doc = new HtmlDocument();
                //doc.LoadHtml(LinksHTML);
                //HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//a[@href]");
                foreach (Match item in matches)
                {
                    string node = item.ToString();
                    //   Console.WriteLine(text);  // outputs HtmlAgilityPack.HtmlNode instead o th actual link!!




                    string link = node.ToString();
                    if (!link.Contains("#") || !link.Contains("support.google"))
                    {



                        string w = item.ToString();
                        w.Remove(8);
                        w.Remove(w.Length - 5);
                        Console.WriteLine("Bonjour");
                        Console.WriteLine(w);
                        //  URLSofCs.Add(w);

                    }


                }

            }

            else
            {
                Console.WriteLine("No links could be found ");
                Console.WriteLine("An error Occured");
            }


What I have tried:

The above is code I have already tried , the HTML AGILITY bit of code in the comments doesn't work either in what I am trying to achieve
this code gives an output but I have no idea what it outputs

Ps
Title can be whatever the user inputs or something collected from before


UIK Some c# but am still learning so any help would be appreciated as its for a pretty big part of my final grade
Posted
Comments
Maciej Los 20-Dec-19 13:36pm    
What's your issue?
In this question (Collect links from websearch[^]) you were almost on the track...
HamzaMcBob 20-Dec-19 13:47pm    
I have no idea how to make it work when I run it, It gives me something similar to this

<title>Jaden Moodie murder: Man guilty of killing boy in gang knife attack - Google Search(function(){document.documentElement.addEventListener("submit",function(b){var a;if(a=b.target){var c=a.getAttribute("data-submitfalse");a="1"==c||"q"==c&&!a.elements.q.value?!0:!1}else a=!1;a&&(b.preventDefault(),b.stopPropagation())},!0);}).call(this);(function(){var a=window.performance;window.start=(new Date).getTime();a:{var b=window;if(a){var c=a.timing;if(c){var d=c.navigationStart,e=c.responseStart;if(e>d&&e<=window.start){window.start=e;b.wsrt=e-d;break a}}a.now&&(b.wsrt=Math.floor(a.now()))}}window.google=window.google||{};google.aft=function(f){f.setAttribute("data-iml",+new Date)};}).call(this);(function(){window.jsarwt=function(){return!1};}).call(this);(function(){var c=[],e=0;window.ping=function(b){-1==b.indexOf("&zx")&&(b+="&zx="+(new Date).getTime());var a=new Image,d=e++;c[d]=a;a.onerror=a.onload=a.onabort=function(){delete c[d]};a.src=b};}).call(this);body{margin:0 auto;max-width:736px;padding:0 8px}a{color:#1967D2;text-decoration:none;tap-highlight-color:rgba(0,0,0,.1)}a:visited{color:#4B11A8}a:hover{text-decoration:underline}img{border:0}html{font-family:Roboto,HelveticaNeue,Arial,sans-serif;font-size:14px;line-height:20px;text-size-adjust:100%;color:#3C4043;word-wrap:break-word;background-color:#fff}.bRsWnc{background-color:#fff;border-top:1px solid #e0e0e0;height:39px;overflow:hidden}.N6RWV{height:51px;overflow-scrolling:touch;overflow-x:auto;overflow-y:hidden}.Uv67qb{box-pack:justify;font-size:12px;line-height:37px;justify-content:space-between;justify-content:space-between}.Uv67qb a,.Uv67qb span{color:#757575;display:block;flex:none;padding:0 16px;text-align:center;text-transform:uppercase;}span.OXXup{border-bottom:2px solid #4285f4;color:#4285f4;font-weight:bold}a.eZt8xd:visited{color:#757575}.FElbsf{border-left:1px solid rgba(0,0,0,.12)}header article{overflow:visible}.Pg70bf{height:39px;display:box;display:flex;display:flex;width:100%}.H0PQec{position:relative;flex:1}.sbc{display:flex;width:100%}.Pg70bf input{margin:2px 4px 2px 8px;}.x{width:26px;color:#757575;font:27px/38px arial, sans-serif;line-height:40px;}#qdClwb{flex:0 0 auto;width:39px;height:39px;border-bottom:0;padding:0;border-top-right-radius:8px;background-color:#3b78e7;border:1px solid #3367d6;background-image:url(data:image/gif;base64,R0lGODdhJAAjAPIHAODr/nCk+MPZ/FmV96zK+/7+/5K5+kqL9iwAAAAAJAAjAEADani63P4wykmbKcQRXDscQAEMXmmeaLQVLCukzyC09AjfeK7v/MAajACLhPMVAgwjsUcEiZa8xgAYrVqv2Kx2iwsIAAABknfBBAKrTE4IcMyot8ur8datqIbQfJdnAfo2WE6BV05wXIiJigkAOw==);}.sc{font-size:;position:absolute;top:39px;left:0;right:0;box-shadow:0px 2px 5px rgba(0,0,0,0.2);z-index:2;background-color:#fff}.sc>div{padding:10px 10px;padding-left:16px;padding-left:14px;border-top:1px solid #DFE1E5}.scs{background-color:#f5f5f5;}.noHIxc{display:block;font-size:16px;padding:0 0 0 8px;flex:1;height:35px;outline:none;border:none;width:100%;-webkit-tap-highlight-color:rgba(0,0,0,0);overflow:hidden;}.sbc input[type=text]{background:none}.sml .cOl4Id{display:none}.l{display:none}.sml header{background:none}.sml .l{display:block;padding:0 8px}.sml .l{letter-spacing:-1px;text-align:center;border-radius:2px 0 0 0;font:22px/36px Futura, Arial, sans-serif;font-smoothing:antialiased}.bz1lBb{background:#fff;border-radius:8px 8px 0 0;box-shadow:0 1px 6px rgba(32, 33, 36, 0.18);margin-top:10px}.KP7LCb{border-radius:0 0 8px 8px;box-shadow:0 2px 3px rgba(32, 33, 36, 0.18);margin-bottom:10px;overflow:hidden}.cOl4Id{letter-spacing:-1px;text-align:center;font:22pt Futura, Arial, sans-serif;padding:10px 0 5px 0;height:37px;font-smoothing:antialiased}.cOl4Id span{display:inline-block}.S591j{height:100%}.V6gwVd{color:#4285F4}.iWkuvd{color:#EA4335}.cDrQ7{color:#fbcc05}.ntlR9{color:#34A853}.tJ3Myc{-webkit-transform:rotate(-20deg);position:relative;left:-1px
HamzaMcBob 20-Dec-19 13:48pm    
which I cant regex because the links I need are not there

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