|
|
发表于 2016-2-26 16:17:11
|
显示全部楼层
- #region 抓取全站链接% t% {# [) |& }, r! K
- public static List<string> GetAllHref(string url)( B! q' A1 c0 g4 s1 N
- {
7 r6 }5 |2 h) F( [+ b - List<string> allHref = new List<string>();
7 |/ |7 \3 ?/ k1 p4 c - try' H( ^7 r$ U1 V. e4 j' I3 n9 H
- {) a! c" c+ d h# n
- string strhtml = soso.getHtml(url, "", true);2 G' _1 m. Z$ C- @( Z! C) S
- if (strhtml != "error")
' Q# @7 w# f! u; ^/ k6 W4 m5 e6 R - {
& L) m7 H, O5 I) l) a2 y - Regex reg = new Regex(@"(?is)<a[^>]*?href=(['""]?)(?<url>[^'""\s>]+)\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>");) G6 q3 `+ Q. O
- MatchCollection mc = reg.Matches(strhtml);' F s. N/ z; f+ }
- foreach (Match m in mc)
4 n" W+ x u, n4 w6 A1 T - {
( Z6 K7 O# w0 ~) M - Uri uri = new Uri(url);$ K3 P/ F7 q( o
- Uri thisUri = new Uri(uri, m.Groups["url"].Value);. w! w9 b3 {- ~7 {/ _: \
- string fullUrl = "";! }# _. J3 d/ q8 A
- if (m.Groups["url"].Value.StartsWith("http"))
: \# i+ |6 V/ _2 P - {
7 i5 g# I, n w& k: f+ q9 G - fullUrl = m.Groups["url"].Value;& E- V* t/ z3 ?7 O+ B; @3 X
- }: x5 U! o2 B. U% U" K# g3 B' J
- else
7 ?+ m C( p% N" \/ A - {) {1 ~' a6 B( `% F1 E* X
- fullUrl = thisUri.ToString();
9 {. S/ J0 O2 V" _ - }# M7 B0 y6 O/ O; D8 }/ ]9 m
- allHref.Add(fullUrl);
9 j3 K8 m# }' y% P1 a. Z/ k$ q - //Console.WriteLine("原链接:" + m.Groups["url"].Value);& s% ?& t% E" {! m Q
- //Console.WriteLine("文本标记:" + m.Groups["text"].Value);; @8 ~! L+ }2 {8 W" b6 o
- //Console.WriteLine("补全链接:" + fullUrl);% Z# o) j6 q$ J7 \
- //Console.WriteLine("…………………………………………");
3 V2 I* p* Z5 L/ C! T7 \ - }
+ X7 i; v8 N+ w$ Q6 p% l2 \ - }2 ^ o* S W4 \8 j8 z
- }
& P* ?1 i% `! e/ L( ^6 p, m- i& \7 q - catch (Exception ex)
; H6 G4 ^$ n" | V6 |/ W* y7 u - { }3 O( L7 r- x/ |6 f5 F) i; ?
- return allHref;; i" |. B' O% G7 ~ e. {- ~* K8 D
- }
) p' u* _6 P5 s8 O6 E& J! D - #endregion
复制代码 1 m- V; F! m c& N" a
2 n( c: r6 f8 r, y; z2 z# {6 m, J
# E9 W3 [$ g* m. F0 @! j9 [
! J8 \% h& U5 w( S* p0 J
# P: `: q/ b2 V2 \8 e- #region 数据去重/ y! f' m" T4 {8 ?4 T" x
- /// <summary>3 R. h6 w* T- }
- /// List<string>去重; r/ O2 D9 {. Q" Z; C( P" j# B
- /// </summary>+ I4 K& P% q0 t: u
- /// <param name="list"></param>. d% `, w7 \% A3 j; I/ c% L8 [
- /// <returns></returns>
% i, Z0 V$ Y) l% _ - public static List<string> getUnqueList(List<string> list)
' R5 N/ W% u" k9 v; T5 O, i7 N5 R - {" X7 G4 Q5 b, I- Y" B* u) O
- List<string> list1 = new List<string>();$ F3 f( H5 E7 H
- Hashtable hash = new Hashtable();7 N7 [7 t/ z; n
- foreach (string s in list)! H j4 |! s% v
- {
: a( y1 M# A. C' U" h( u& v - if (!hash.ContainsKey(s))
4 _3 Y1 l$ F# h, b: l - {
+ [" o0 \3 G5 ?- g: P2 Y. n' N - hash.Add(s, s);
5 P" c3 K1 d9 t5 K; D P8 U- m - list1.Add(s);; O7 T! N& Y, n1 ?
- }( G% ^! L. \6 I% h( }& P) l
- }
% f2 x d) V5 F8 V& C4 Z' ?9 [ - hash.Clear();
2 M, x N. p1 l - hash = null;" Q' Z- @8 v& x9 v' W
- return list1;: m C8 g! ?; n$ F/ Y% `, t+ R6 B' {
- }7 ]' I. _9 G; w6 h8 ?
- #endregion
复制代码 6 m" R, Z/ s0 V! d
/ K; N4 r( _- x0 | |
|