Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Iyouboushi
Sunday, January 27th, 2008 at 3:52:38am UTC 

  1.         public String getResult(String query)
  2.         {
  3.             String result = "";
  4.  
  5.             try
  6.             {
  7.                 // First things first, let's set up what we're searching for.
  8.                 String googleUrl = "http://www.google.com/search?q=" + query;
  9.  
  10.                 // Now we'll grab the results page and store it into memory
  11.                 String html = GetHtml(googleUrl);
  12.  
  13.                 // It's an HTML file and we don't want all the HTML stuff.  We just want our result.
  14.  
  15.                 /////////////////////////////////////////////
  16.                 // This is what's messed up.  The information I want is located in the HTML file as:
  17.                 // <font size=-1><b>a title</b> - WHAT I WANT <br>
  18.                 // having trouble figuring out what regEx pattern I need to have in order to pull that bit out.
  19.                 /////////////////////////////////////////////
  20.                 //String regExPattern = (@"<font[^>]*-1><b>.*<[^>]*b>.*?<br>");              <-- previous attempt, didn't work
  21.                 //String regExPattern = (@"<font[^>]*-1><b>*?</b>(?<content>.*?)<br>");      <-- previous attempt, didn't work
  22.  
  23.                 // Current pattern.  Returns 1 match, which is the first instance of <font size=-1> and then includes the ENTIRE
  24.                 // rest of the HTML page.  So in essence, it's still not working right.  For an image of what it's doing, what it
  25.                 // SHOULD do, go here:  http://img401.imageshack.us/img401/3479/regexxd8.jpg
  26.                 String regExPattern = @"<font\s*size=\-1><b>[\w\d\s]+<\/b>\ *(.*[^\s])\s*<br>";
  27.  
  28.                 Regex r = new Regex(regExPattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
  29.  
  30.                 // Search for our match
  31.                 MatchCollection matches = r.Matches(html);
  32.  
  33.                 // How many matches did we find anyway?
  34.                 int amountOfMatches = matches.Count;
  35.  
  36.                 // temp
  37.                 Console.WriteLine(amountOfMatches);
  38.  
  39.                 // If it found any matches we should randomly pick one. 
  40.                 if (amountOfMatches > 0)
  41.                 {
  42.                     Random randChance = new Random();
  43.                     int random;
  44.                     if (amountOfMatches > 200)
  45.                         amountOfMatches = 101;
  46.  
  47.                     random = randChance.Next(0, amountOfMatches - 1);
  48.  
  49.  
  50.                     Console.WriteLine(random); // temp
  51.                     result = matches[random].ToString();
  52.                 }
  53.             }
  54.             catch (Exception e)
  55.             {
  56.                 System.Console.WriteLine("Error: " + e.ToString());
  57.             }
  58.  
  59.             // Return the results.
  60.             return result;
  61.         }
  62.  
  63.  
  64.  
  65.         private static String GetHtml(string url)
  66.         {
  67.             using (StreamReader sr = new StreamReader(GetStreamFromUrl(url)))
  68.             {
  69.                 return sr.ReadToEnd();
  70.             }
  71.         }
  72.  
  73.         private static Stream GetStreamFromUrl(string url)
  74.         {
  75.             return new WebClient().OpenRead(url);
  76.         }

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right
worth-right
worth-right