html中怎么找回消失的源代码,如何获取网页源代码中无法显示的内容??

usingHtmlAgilityPack;usingSystem;usingSystem.IO;usingSystem.Net;usingSystem.Text;usingSystem.Threading;namespaceHtmlAnalyzeDemo

{public classHttpWebRequestBeginGetRequest

{private static ManualResetEvent allDone = new ManualResetEvent(false);static string postData = "{\"cateIds\":\"108698,2,108701,108703,108704,108705,108709,108712,108724,4\"}";private static string html = "";public static void Main(string[] args)

{

HttpWebRequest request= (HttpWebRequest)WebRequest.Create("http://www.cnblogs.com/aggsite/SubCategories");

request.Accept= "text/plain, */*; q=0.01";

request.Host= "www.cnblogs.com";

request.ContentType= "text/plain;charset=UTF-8";

request.ContentLength=postData.Length;

request.Method= "POST";//异步请求子菜单

request.BeginGetRequestStream(newAsyncCallback(GetRequestStreamCallback), request);

allDone.WaitOne();//将获取到的源代码转为流

byte[] array =Encoding.UTF8.GetBytes(html);

MemoryStream stream= newMemoryStream(array);

StreamReader reader= newStreamReader(stream);

HtmlDocument htmlDocumentSub= newHtmlDocument();//加载html成xml

htmlDocumentSub.Load(reader);//获取html首页的一级菜单

HtmlDocument htmlDocument = new HtmlWeb().Load("http://www.cnblogs.com");

HtmlNodeCollection htmlNodes= htmlDocument.DocumentNode.SelectNodes("//ul[@id='cate_item']/li");foreach (var node inhtmlNodes)

{string onmouseover = node.Attributes["onmouseover"].Value;string cateshowId=onmouseover.Split(new char[]{'(',')'})[1];

HtmlNodeCollection htmlCateNodes= htmlDocumentSub.DocumentNode.SelectNodes("//div[@id='cate_content_block_" + cateshowId + "']/div/ul/li");

Console.WriteLine("--" +node.InnerText);if (htmlCateNodes != null)

{foreach (var cateNode inhtmlCateNodes)

{

Console.WriteLine("----"+cateNode.InnerText);

}

}

}//Console.WriteLine(htmlDocumentSub.DocumentNode.InnerHtml);

}private static voidGetRequestStreamCallback(IAsyncResult asynchronousResult)

{

HttpWebRequest request=(HttpWebRequest)asynchronousResult.AsyncState;

Stream postStream=request.EndGetRequestStream(asynchronousResult);byte[] byteArray =Encoding.UTF8.GetBytes(postData);

postStream.Write(byteArray,0, postData.Length);

postStream.Close();//请求完成后异步读取数据

request.BeginGetResponse(newAsyncCallback(GetResponseCallback), request);

}private static voidGetResponseCallback(IAsyncResult asynchronousResult)

{

HttpWebRequest request=(HttpWebRequest)asynchronousResult.AsyncState;

HttpWebResponse response=(HttpWebResponse)request.EndGetResponse(asynchronousResult);

Stream streamResponse=response.GetResponseStream();

StreamReader streamRead= newStreamReader(streamResponse);string responseString =streamRead.ReadToEnd();//请求完成的字符串保存到html中

html =responseString;

streamResponse.Close();

streamRead.Close();

response.Close();

allDone.Set();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值