VB.net 2010 视频教程 VB.net 2010 视频教程 VB.net 2010 视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > C#编程 >
  • C#教程之C#获取页面上的Html

  • 2019-07-14 22:37 来源:未知
 
复制代码
 1 //根据Url地址得到网页的html源码 
 2 public static string GetWebContent(string Url)
 3 {
 4   string strResult = "";
 5   try
 6   {
 7     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
 8     //声明一个HttpWebRequest请求 
 9     request.Timeout = 30000;
10     //设置连接超时时间 
11     request.Headers.Set("Pragma", "no-cache");
12     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
13     Stream streamReceive = response.GetResponseStream();
14     Encoding encoding = Encoding.GetEncoding("GB2312");
15     StreamReader streamReader = new StreamReader(streamReceive, encoding);
16     strResult = streamReader.ReadToEnd();
17   }
18   catch
19   {
20     Console.WriteLine("出错");
21   }
22     return strResult;
23   }
24 
25  
26 
27 public string Search(string argAddress)
28 {
29   //要抓取的URL地址 
30   StringBuilder sb = new StringBuilder();
31   //sb.Append("http://ditu.google.cn/maps");
32   //sb.Append("?");
33   //sb.Append("hl=zh-CN&newwindow=1&safe=strict");
34   //sb.Append("&q=");
35   //sb.Append(argAddress);
36   //sb.Append("&bav=on.2,or.&bvm=bv.44158598,d.dGI&biw=1440&bih=775&um=1&ie=UTF-8&sa=N&tab=wl");
37   sb.Append("http://query.customs.gov.cn/MNFTQ/MQuery.aspx");
38 
39   //得到指定Url的源码 
40   string strWebContent = GetWebContent(sb.ToString());
41 
42   ////生成HtmlDocument 
43   //WebBrowser webb = new WebBrowser();
44   //webb.Navigate("about:blank");
45   //HtmlDocument htmldoc = webb.Document.OpenNew(true);
46   //htmldoc.Write(strWebContent);
47   //HtmlElementCollection htmlTR = htmldoc.GetElementsByTagName("TR");
48   //foreach (HtmlElement tr in htmlTR)
49   //{
50     // string address = string.Empty;
51 
52 
53     // try
54     // {
55       // string resultspanel = tr.Document.GetElementById("resultspanel").Document.GetElementById("panel_A_2").InnerText;
56 
57       // string[] ContentLines = resultspanel.Split(new string[] { "\r\n" }, StringSplitOptions.None);//不忽略空行
58 
59       // address = ContentLines[1];
60     // }
61     // catch { };
62 
63 
64     // //插入DataTable 
65     // if (address != string.Empty)
66     // {
67       // return address;
68     // }
69     // else
70     //  {
71       // continue;
72     // }
73   //}
74 
75   return strWebContent;
76 }
复制代码

 

在学习的过程中记录一些笔记及经验 如有疑问请咨询https://wpa.qq.com/msgrd?v=3&uin=2861510776&site=qq&menu=yes

相关教程