日期:2014-05-20 浏览次数:20937 次
WebBrowser web = new WebBrowser(); web.Navigate("http://www.xjflcp.com/ssc/"); web.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(web_DocumentCompleted); void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { WebBrowser web = (WebBrowser)sender; HtmlElementCollection ElementCollection = web.Document.GetElementsByTagName("Table"); foreach (HtmlElement item in ElementCollection) { File.AppendAllText("Kaijiang_xj.txt", item.InnerText); } }
------解决方案--------------------
//先取网页HTML string r; WebRequest myReq = WebRequest.Create("网页URL"); WebResponse myRes = myReq.GetResponse(); Stream resStream = myRes.GetResponseStream(); StreamReader sr = new StreamReader(resStream, Encoding.Default); StringBuilder sb = new StringBuilder(); while ((r = sr.ReadLine()) != null) { sb.Append(r); } //再利用正则表达 string regexStr = @"正则表达式"; Match mc = Regex.Match(sb.ToString(), regexStr, RegexOptions.IgnoreCase); this.Label1.Text = mc.Groups[1].Value; myRes.Close();
------解决方案--------------------
WebClient web = new WebClient(); byte [] buffer = web.DownloadData("http://www.xjflcp.com/ssc/"); string html = System.Text.UTF8Encoding.UTF8.GetString(buffer);
------解决方案--------------------
/// <summary> /// 抓取网页前台源码 /// </summary> /// <param name="url"></param> /// <returns></returns> public string GetPageContent(string url) { string ContentHtml = String.Empty; HttpWebRequest rt = null; HttpWebResponse rs = null; Stream stream = null; StreamReader sr = null; try { rt = (HttpWebRequest)WebRequest.Create(url); rs = (HttpWebResponse)rt.GetResponse(); stream = rs.GetResponseStream(); sr = new StreamReader(stream, System.Text.Encoding.Default); ContentHtml = sr.ReadToEnd(); sr.Close(); stream.Close(); rs.Close(); } catch (Exception ex) { ContentHtml = "no values"; } finally { if (rs != null) rs.Close(); if (stream != null) stream.Close(); if (sr != null) sr.Close(); } return ContentHtml; }