|
|
发表于 2016-2-26 16:23:12
|
显示全部楼层
6 @# `% t& z* R( [) g
缺少一个gethtml,用下面这个:( E$ l( ^" V6 A8 ?
- public string getHtml(string url, string charSet, bool UseUTF8CharSet)//url是要访问的网站地址,charSet是目标网页的编码,如果传入的是null或者"",那就自动分析网页的编码 ) S7 r* f2 g3 i0 R4 b8 U
- {2 R+ j* a: O2 ]' k5 t& ^# l% v4 O
- string strWebData = "error";/ {* s: Z& I* Y
- try
- i; ^5 a' i4 ?! _ - {
2 ]6 y4 d N* [# i8 G) Z$ d% C* l ]( X8 ? - WebClient myWebClient = new WebClient(); //创建WebClient实例myWebClient z$ ]0 }0 ~& k) I3 t% Q: P
- // 需要注意的:
6 ]( \9 _0 o0 A, K l - //有的网页可能下不下来,有种种原因比如需要cookie,编码问题等等 6 D6 e+ f5 A! K& R2 d3 u
- //这是就要具体问题具体分析比如在头部加入cookie ) W8 }4 @$ H) z% f B
- // webclient.Headers.Add("Cookie", cookie);
' }$ K1 g' S# c7 ~ - //这样可能需要一些重载方法。根据需要写就可以了' `$ o, Z8 |9 m
- 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)");+ `+ }3 `; O0 N1 q' D
- //myWebClient.Headers.Add("User-agent", "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");+ X; W% d8 n+ v2 d* O
- //获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
5 k) n# X) P @9 y - myWebClient.Credentials = CredentialCache.DefaultCredentials;
( L, Z V6 _: l. z - //如果服务器要验证用户名,密码
5 B4 K: O- F% s: O4 q8 ~ - //NetworkCredential mycred = new NetworkCredential(struser, strpassword);
m$ V3 J: w1 G7 ?+ L5 Q9 U9 p4 B - //myWebClient.Credentials = mycred;
. X8 s$ {) Y5 q0 Z - //从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号)
2 B" G# u8 U- L, d - byte[] myDataBuffer = myWebClient.DownloadData(url);
/ H1 a& u6 H" A$ J9 L - strWebData = Encoding.Default.GetString(myDataBuffer);
& i4 R5 ?( C4 o! l3 _; J/ T' P - 1 q8 ?; ~2 W8 Z. \: {& {
- //获取网页字符编码描述信息
) h: @6 o4 ?$ T. j: u8 H - Match charSetMatch = Regex.Match(strWebData, "<meta([^<]*)charset=([^<]*)"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
: @' q! T! x0 ^$ E8 z, y/ [ - string webCharSet = charSetMatch.Groups[2].Value;; l( f5 E; `# f. M) e0 b0 Z
- if (charSet == null || charSet == "")' O6 e! k* w6 k& K0 l M+ I# S2 o
- charSet = webCharSet;
+ P8 O5 S6 N1 ~0 D; r B; n7 ]0 P) g - if (charSet.Length > 0)2 R/ s* D" j) U
- {
+ k# C5 `' J/ m' K - charSet = charSet.Replace(""", "");
- [$ b. d1 \2 b3 b9 s - }
% Q+ m3 o. K! U, {. d, ^8 g - if (UseUTF8CharSet)* n/ O9 s" F( X- y" x- `
- {
* r2 v3 k5 C+ \5 K n - if (charSet == null || charSet.Length == 0)( v% l$ P4 j* P: I! u$ j* k* q1 X- M+ @
- {
6 [" P: _5 e* w+ D- D& g6 n - charSet = "utf-8";" ~0 o! n8 q- b( u
- }
$ [4 d* \2 c, w/ n: P0 P7 O6 k - }
! B7 U: e9 W* O - if (charSet != null && charSet != "" && Encoding.GetEncoding(charSet) != Encoding.Default)
" c n! w8 y) Z2 } - strWebData = Encoding.GetEncoding(charSet).GetString(myDataBuffer);
7 c% D) ~- }" o7 e }: f& D. Y
* N, q4 @( a3 h- l7 ?/ M. }- }
* ]. F8 u4 m, b8 z/ o7 S, P f - catch (Exception)0 \6 H1 B9 e9 `+ w( m8 a
- {; O, e4 S) P' r7 U- g
- strWebData = "error";% G, D8 ~% s7 ~ F- L2 h
- }
- G+ e6 L" `7 T/ E - ! `( `! L$ u7 @
- return strWebData;
, N. \" s3 B0 Y8 X6 y) o N - }
复制代码
* u. w8 d' E* \4 _( z2 a' ?+ L3 C7 c; h/ Z- v' o. T# b
|
|