|
|
发表于 2016-2-26 16:23:12
|
显示全部楼层
5 @% r% T# G; P: G! N缺少一个gethtml,用下面这个:: p0 p! h. w, \' L
- public string getHtml(string url, string charSet, bool UseUTF8CharSet)//url是要访问的网站地址,charSet是目标网页的编码,如果传入的是null或者"",那就自动分析网页的编码 6 w9 R! G. \/ [6 g
- {" b! w1 q+ ?2 L7 T5 D5 `1 p6 `
- string strWebData = "error";! J3 V5 E5 U' w2 {' n4 J l0 n
- try
' P J( P$ m0 |8 @ - {+ u5 B& P1 k6 \9 W k b
- WebClient myWebClient = new WebClient(); //创建WebClient实例myWebClient
' q' z# [& L5 }6 i/ [ - // 需要注意的: 9 o. |. t% m6 q# {9 ]4 c
- //有的网页可能下不下来,有种种原因比如需要cookie,编码问题等等
( [# }6 Y/ ]; ^, B7 g - //这是就要具体问题具体分析比如在头部加入cookie + v& k! Q/ X3 \! |7 ]
- // webclient.Headers.Add("Cookie", cookie); ; G1 {/ u% K4 t. D/ M
- //这样可能需要一些重载方法。根据需要写就可以了$ i6 e9 k; Y k: \
- myWebClient.Headers.Add("User-agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)");5 k G3 C9 T5 `7 Z! t
- //myWebClient.Headers.Add("User-agent", "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");7 {+ s7 l* G& W- d9 w( B# G* ^
- //获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
|5 z/ j5 @. C; h7 X% x - myWebClient.Credentials = CredentialCache.DefaultCredentials;9 v. S; k( B# R
- //如果服务器要验证用户名,密码
( S( C f; V5 ?* m* h9 }; y% Q m - //NetworkCredential mycred = new NetworkCredential(struser, strpassword);
% ?" n t. E7 B+ [4 m: ^ - //myWebClient.Credentials = mycred; B" k G: a! h, n" r) ]
- //从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号) ( Z: d4 C8 l/ e6 ?: f {
- byte[] myDataBuffer = myWebClient.DownloadData(url);
" _4 v. o u& z( E9 N, x - strWebData = Encoding.Default.GetString(myDataBuffer);" f1 U( y. G2 x$ ~4 ]3 H4 f
! t' O- u, b5 t# R+ g! h0 _- Z- //获取网页字符编码描述信息
+ _8 C$ V- A1 c5 ?4 P - Match charSetMatch = Regex.Match(strWebData, "<meta([^<]*)charset=([^<]*)"", RegexOptions.IgnoreCase | RegexOptions.Multiline);% ~ Q4 x7 Z3 ]& I
- string webCharSet = charSetMatch.Groups[2].Value;
, q+ v m, L3 O y - if (charSet == null || charSet == "")
! t9 T7 N& @* Z2 P5 d( ] - charSet = webCharSet;& H+ e5 } _, c! i; A% @
- if (charSet.Length > 0)
1 w8 Y9 O* C4 K - {( O/ K6 Q/ E" D! W6 h9 ]
- charSet = charSet.Replace(""", "");9 |/ X; ?! r' W4 L8 [0 g! l
- }. ]% y) s4 O+ D, f9 U% a
- if (UseUTF8CharSet)
. a; K0 c; r9 [ - {
6 X7 a; U' Z% l - if (charSet == null || charSet.Length == 0), C$ M1 \: [2 o# {$ J* n: [4 c8 L+ x
- {* p& k0 s9 t7 _( x3 ~ h% f+ Z
- charSet = "utf-8";6 ]/ j# k* N! ]; E( _! K
- }7 c6 R3 o( a8 R7 N$ z
- }
- {- L2 h f4 g3 P$ }. D - if (charSet != null && charSet != "" && Encoding.GetEncoding(charSet) != Encoding.Default). Q7 _9 K- E. Z/ K I! D
- strWebData = Encoding.GetEncoding(charSet).GetString(myDataBuffer);4 \* y' Y. G% m% {3 `
- 5 l/ v0 L6 c( D& u* x I# ?9 N) P
- }& R2 w2 c$ v5 a, A w
- catch (Exception), o5 A; _5 L7 C/ C5 U& W8 {) W/ `
- {6 K3 x2 K# q# y; _
- strWebData = "error";
9 y- L4 z- c' x! r7 n' b8 r0 U - }
# m- r% y- l& N& l) G# \" h. _2 W
' S* l/ ~; c9 Q0 F; B- b: ]! F( p- return strWebData;( ]. @ G4 n, Y" K2 m2 [/ }
- }
复制代码 7 L7 C* J5 v/ C3 }6 r, X; X# Y
7 L# G1 f h" g2 D: k" d/ l |
|