use google

using System; using System.IO; using System.Net; using System.Text.RegularExpressions; namespace Scraper { class GoogleHit { private static string REGEX_PAT = @" (.*?)
(.*)
.*?/s+-/s+(/d+/w)/s+.*? Cached.*? Similar pages"; private string url; private string title; private string desc; private string size; private string cache; private string related; public GoogleHit (string html) { Regex regex = new Regex (REGEX_PAT, RegexOptions.IgnoreCase); Match m = regex.Match (html); if (m.Success) { url = m.Groups[1].ToString (); title = m.Groups[2].ToString (); desc = m.Groups[3].ToString (); size = m.Groups[4].ToString (); cache = m.Groups[5].ToString (); related = "http://www.google.com" + m.Groups[6].ToString (); } else Console.WriteLine ("i don't get it."); } public override string ToString () { string str = "url [" + url + "]/n" + "title [" + title + "]/n" + "desc [" + desc + "]/n" + "size [" + size + "]/n" + "cache [" + cache + "]/n" + "related [" + related + "]"; return str; } } class Scraper { private static string HIT_SEPARATOR = "

"; private static string HIT_TERMINATOR = "Similar pages "; private static int MAX_RESULTS = 10; public static GoogleHit[] QueryGoogle (string queryString) { char[] query = queryString.ToCharArray (); for (int i=0; i<query.Length; ++i) if (Char.IsWhiteSpace (query[i])) query[i] = '+'; string url = "http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=" + new string (query) + "&btnG=Google+Search"; Console.WriteLine (String.Format ("URL: {0}", url)); HttpWebRequest req = (HttpWebRequest)WebRequest.Create (url); req.UserAgent = "mozilla"; WebResponse resp = req.GetResponse (); StreamReader input = new StreamReader (resp.GetResponseStream ()); string buf; int pivot = 0; bool feof = false; // eat stream until first HIT_SEPARATOR do { buf = input.ReadLine (); if (buf == null) feof = true; else { pivot = buf.IndexOf (HIT_SEPARATOR); Console.WriteLine (String.Format ("buf: {0}", buf)); } } while (pivot < 0 && !feof); if (feof) { Console.WriteLine ("EOF"); return null; } buf = buf.Substring (pivot + HIT_SEPARATOR.Length); GoogleHit[] hits = new GoogleHit[MAX_RESULTS]; for (int i=0; i<MAX_RESULTS; ++i) hits[i] = null; for (int i=0; i<MAX_RESULTS && !feof; ++i) { pivot = buf.IndexOf (HIT_SEPARATOR); while (pivot < 0 && !feof) { string line = input.ReadLine (); if (line != null) buf += line; else feof = true; pivot = buf.IndexOf (HIT_SEPARATOR); } if (pivot < 0) pivot = buf.IndexOf (HIT_TERMINATOR) + HIT_TERMINATOR.Length; string html = buf.Substring (0, pivot); hits[i] = new GoogleHit (html); buf = buf.Substring (pivot + HIT_SEPARATOR.Length); } return hits; } public static void Main (string[] args) { GoogleHit[] hits = QueryGoogle (args[0]); if (hits == null) { Console.WriteLine ("no hits"); return; } for (int i = 0; i < MAX_RESULTS; ++i) { if (hits[i] != null) { Console.WriteLine (hits[i]); Console.WriteLine (); } } } } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值