|
|
发表于 2016-2-26 16:17:11
|
显示全部楼层
- #region 抓取全站链接
6 n- _. u6 \. n. p - public static List<string> GetAllHref(string url)
! g* z! I+ x( M. r; x- n - { S/ _# j' s. C3 k4 \
- List<string> allHref = new List<string>();5 {( W {1 P6 m, x' ~8 o
- try) n' ~6 ? I3 k' u+ x5 J6 Z2 K
- {
4 h: x l$ o% r- E. ~% P! ], L2 G - string strhtml = soso.getHtml(url, "", true);
& S' h# { T; i7 Y: ]' E, ~ - if (strhtml != "error")5 G( ^: A1 e* J) n
- {
/ r) T8 `# g$ @% r - Regex reg = new Regex(@"(?is)<a[^>]*?href=(['""]?)(?<url>[^'""\s>]+)\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>");8 j9 r# G& [' z) u7 p3 m
- MatchCollection mc = reg.Matches(strhtml);
% W& F" C, ~9 A$ L - foreach (Match m in mc)
1 D' d" o$ W. {6 X6 U! I% v - { t S, G- w4 D6 X# ~
- Uri uri = new Uri(url);
* j+ i/ Q% M- B) r' S u - Uri thisUri = new Uri(uri, m.Groups["url"].Value);& I/ H) T/ u/ Q/ S6 ^0 r
- string fullUrl = "";* }/ R2 G, k! p1 u
- if (m.Groups["url"].Value.StartsWith("http")), p+ H& p6 c! H2 M, y* w
- {
) F6 T; f9 O8 g) H U - fullUrl = m.Groups["url"].Value;
9 U: j: G% X( L, Z* r - }
& _, Z: }, f& N/ S - else
' Z$ r* Y" h2 i& d' e+ {1 L - {; q4 u3 u, q; o7 }9 m% P
- fullUrl = thisUri.ToString();$ A0 e- U( p* x
- }7 r1 L. {; N# A8 q6 k; {. m- L
- allHref.Add(fullUrl);
* r. n6 C e8 I - //Console.WriteLine("原链接:" + m.Groups["url"].Value);
( [- K* G: w6 }2 v6 Q! H - //Console.WriteLine("文本标记:" + m.Groups["text"].Value);
8 {" l0 b' [% L0 V - //Console.WriteLine("补全链接:" + fullUrl);' @* \0 d3 t2 r* g$ g! ]7 h b% Z
- //Console.WriteLine("…………………………………………");
1 R5 O% f) y2 i: h' ?% T0 X - } M8 m) o0 W2 ^$ q- @
- }
& L) B+ B# H+ r+ M0 B - }% R+ j1 R* }2 v8 T
- catch (Exception ex)
4 z6 L: q+ o3 N# Z1 W - { }! k7 z8 h9 P. L# F# U' I
- return allHref;" K+ x# W& D) f+ z- D1 |" a3 d; C' g
- }% a; w6 s! P* ~7 j( i! n
- #endregion
复制代码 ( }" V1 |+ `8 E. T3 t
# D) T1 A# J& Y& G
- ! u3 i5 T( G6 c3 v) J' H8 S
- 3 ^, M6 F# I9 r' u7 W/ E1 o! z6 y
- 9 {; _5 D) a5 R. ?% O% G8 D' p
- #region 数据去重2 n, v' a8 U; S- v
- /// <summary>. X- a7 D* h5 V
- /// List<string>去重1 p% }2 r, S% o- U& [$ E
- /// </summary>) r, {( L& v9 u. @
- /// <param name="list"></param>
$ D, T1 | H. Z {! ~: s, R* O - /// <returns></returns>
. `- j2 o8 {! ?. q- W) c( B - public static List<string> getUnqueList(List<string> list)- w0 T7 m, D) Y+ n% n I, d D/ u# K
- {
% |+ y4 w$ Z( _' j3 M1 c9 w - List<string> list1 = new List<string>();' ^/ f8 G. Q3 ^* V/ R
- Hashtable hash = new Hashtable();
& e) N a5 D% G ^8 j$ Q, [ - foreach (string s in list)
- K) Q! }- G8 ~3 d - {! n+ D4 U; g9 Z! z! U: a/ A
- if (!hash.ContainsKey(s))
* K0 b8 Y. T1 S0 H8 ~8 C& ^ - {( ^/ Y; g& p1 P& Q0 \, b/ H+ B
- hash.Add(s, s);; o$ S1 I4 r. C- Y2 j- Z
- list1.Add(s);) k- j& O" M& f' W: y
- }. k' ^3 n! G6 ^6 Y2 a7 c. |3 Y
- }
9 Y! ?$ l* ^1 q2 M/ @ - hash.Clear();; B8 G) z9 o$ ?6 |7 ]$ k+ c b
- hash = null;
+ x6 \3 B( m3 {7 L% e" H4 }: } - return list1;
" Z- F2 j. h" ?3 s - }8 G, E. x5 ^- {* d
- #endregion
复制代码
6 u2 o: S& ^' Y0 Q/ H
* Q! {. ~2 E6 J- M/ h: ]- p |
|