示例#1
0
 public static string FetchPageTitle(string url)
 {
     // TODO: Handle this in a way that is less stupid (start with a HEAD, perhaps)
     try
     {
         WebClient    wc   = new WebClient(); // I'm sorry, okay?
         StreamReader sr   = new StreamReader(wc.OpenRead(url));
         string       data = sr.ReadToEnd();
         sr.Close();
         HtmlDocument hDocument = new HtmlDocument();
         hDocument.LoadHtml(data);
         var title = hDocument.DocumentNode.Descendants("title");
         if (title != null)
         {
             if (title.Count() > 0)
             {
                 string text = title.First().InnerText;
                 text = text.Replace("\n", "").Replace("\r", "").Trim();
                 if (text.Length < 100)
                 {
                     return(WebUtility.HtmlDecode(HtmlRemoval.StripTagsRegexCompiled(text)));
                 }
             }
         }
     }
     catch { return(null); }
     return(null);
 }
示例#2
0
        public static List <string> DoGoogleSearch(string terms)
        {
            List <string> results = new List <string>();

            try
            {
                WebClient    client = new WebClient();
                StreamReader sr     = new StreamReader(client.OpenRead("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" + Uri.EscapeUriString(terms)));
                string       json   = sr.ReadToEnd();
                sr.Close();
                JObject jobject = JObject.Parse(json);
                foreach (var result in jobject["responseData"]["results"])
                {
                    results.Add(WebUtility.HtmlDecode(HtmlRemoval.StripTagsRegexCompiled(Uri.UnescapeDataString(result["title"].Value <string>())) +
                                                      " " + Uri.UnescapeDataString(result["url"].Value <string>())));
                }
            }
            catch (Exception)
            {
            }
            return(results);
        }