Use Google

xiaoxiao2021-03-06  18

Using system; 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 (); dec = M.Groups [3]. Tostring (); size = m.Groups [4] .tostring (); cache = m.Groups [5] .tostring (); related = "http://www.google.com" m.Groups [6] 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_sePare ="

"; 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 (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 untric 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

转载请注明原文地址:https://www.9cbs.com/read-41790.html

New Post(0)