|
|
发表于 2016-2-26 16:23:12
|
显示全部楼层
; z0 x$ I. X7 b5 e j% x; A) X; ?6 W
缺少一个gethtml,用下面这个:& J5 f4 `) N( c0 K
- public string getHtml(string url, string charSet, bool UseUTF8CharSet)//url是要访问的网站地址,charSet是目标网页的编码,如果传入的是null或者"",那就自动分析网页的编码 6 C. ~$ J8 G. ~6 e/ _0 ?# p; b. H
- {1 Q7 [# f# z5 p5 W
- string strWebData = "error";
0 y5 e3 Z/ G9 A. ^ - try1 C" W4 m0 c G2 x0 [4 g
- {
+ g. G$ }% o- T+ M" F9 f - WebClient myWebClient = new WebClient(); //创建WebClient实例myWebClient
5 k3 U Z' j! b4 G9 j' r - // 需要注意的: ; R' \3 g) V6 H& G7 `
- //有的网页可能下不下来,有种种原因比如需要cookie,编码问题等等 9 ] A3 Y: \( m) ^; W; l
- //这是就要具体问题具体分析比如在头部加入cookie
) t6 D3 K, v# t) Z$ u - // webclient.Headers.Add("Cookie", cookie);
D! v; n& y7 R) t; n! n6 O - //这样可能需要一些重载方法。根据需要写就可以了
8 v* F( b& Z, e$ }+ n, T, _- b& ] - 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)");
4 T: I2 |6 H6 c5 \6 ~2 D+ [ - //myWebClient.Headers.Add("User-agent", "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");% Q# E& L/ i8 [) C$ y! ]
- //获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
' `( m" d2 O( |" j - myWebClient.Credentials = CredentialCache.DefaultCredentials;
. J, K+ t2 W9 |5 G0 k - //如果服务器要验证用户名,密码
+ Q9 V f- O+ u4 L$ \! _2 g8 R1 A - //NetworkCredential mycred = new NetworkCredential(struser, strpassword);
% X7 r9 A4 l* q; }- F - //myWebClient.Credentials = mycred; , ^3 |) \* `8 g) ^8 a/ {0 a
- //从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号)
5 c/ z. W" M* j3 d - byte[] myDataBuffer = myWebClient.DownloadData(url);( l* K0 Q \, F7 z9 L3 V2 i
- strWebData = Encoding.Default.GetString(myDataBuffer);( h! N8 D7 P$ ]7 F6 u- S
) j, H' H0 _: J$ g0 x- //获取网页字符编码描述信息
' D5 v H; R# }- z/ `& R. U - Match charSetMatch = Regex.Match(strWebData, "<meta([^<]*)charset=([^<]*)"", RegexOptions.IgnoreCase | RegexOptions.Multiline);+ p! g- i0 c2 |2 ^
- string webCharSet = charSetMatch.Groups[2].Value;
& R- a6 M7 x8 d P: m3 k( d1 H @ - if (charSet == null || charSet == "")
( c& V4 r9 w. f/ s1 s* | - charSet = webCharSet; c9 p0 \ G4 i1 ~8 N, W) m
- if (charSet.Length > 0)
- R% V7 P* }5 i7 k7 ] - {# T3 e a* C b }& U3 u
- charSet = charSet.Replace(""", "");
& [' u, z" m( Q, } - }* L' D6 v* }# o9 q" A( N' h
- if (UseUTF8CharSet)$ W2 p! R. v9 Z
- {
/ L0 P5 Y5 y$ R3 d8 q' Q$ [ - if (charSet == null || charSet.Length == 0)
8 Z* ?. _$ f5 W - {' B% ` I% e/ F
- charSet = "utf-8";
1 J. X+ C" C/ F5 G$ K( R - }
. x8 p& Q P- Y* G9 J# p; b" I - }
9 U" {1 Z/ t7 u* r/ ?* i - if (charSet != null && charSet != "" && Encoding.GetEncoding(charSet) != Encoding.Default): S* J7 A2 r- P
- strWebData = Encoding.GetEncoding(charSet).GetString(myDataBuffer);
% S. Q! J, X- g4 x9 q; T: E - % h( F9 f$ M) c' n+ `
- }: h; M) i, ?8 y0 E; T
- catch (Exception)* _3 `: c5 |& p& M8 c
- {
% n! h& _+ ?. `( o* v0 d) \+ a - strWebData = "error";
# P* @+ ~ Q$ W- P+ l - }% _7 o; S, ~& x2 {# F9 L
- ! S0 D1 P% h) J) q& i
- return strWebData;
$ Q* V6 C. R( M/ n% Z2 W - }
复制代码 & Q) Y L/ c9 c( o; u' J! K- Z5 D+ Q
5 B n4 L0 o6 @/ z( P6 i$ E
|
|