|
|
发表于 2016-2-26 16:17:11
|
显示全部楼层
- #region 抓取全站链接9 W% b; d: l1 G" P$ E
- public static List<string> GetAllHref(string url)
5 h, O, `* x, l3 e3 F# z# } - {, j# Q# O8 t& V$ G' S% K: }# n
- List<string> allHref = new List<string>();
; J2 }, B, [" [" G8 L - try
9 r8 k/ U8 I: @ - {5 D |6 e* a8 E* _* @+ {5 w
- string strhtml = soso.getHtml(url, "", true);8 b6 h/ E6 i' b K) z. t& U
- if (strhtml != "error")
, P$ I+ ]; N# T J$ x" _ - {
2 e6 ?. v0 W; {, ~' R* Q3 c - Regex reg = new Regex(@"(?is)<a[^>]*?href=(['""]?)(?<url>[^'""\s>]+)\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>");+ g& Y. T: s, h* D' ?
- MatchCollection mc = reg.Matches(strhtml);
' B( e2 Y3 n! W' T- ? - foreach (Match m in mc)
" M( }) p7 O& S) ^: m - {
5 Y! o6 L/ G. U. S" o: W, X - Uri uri = new Uri(url);; F; M+ B' z( O$ q
- Uri thisUri = new Uri(uri, m.Groups["url"].Value);7 }* p @+ N2 V- N9 u7 W1 _
- string fullUrl = "";
6 k1 H+ v$ ^4 { O" V$ k$ @0 o5 D - if (m.Groups["url"].Value.StartsWith("http"))
5 C: y# r( ]" W+ Q+ O3 p - {
7 W+ }) q& T; I. |& q - fullUrl = m.Groups["url"].Value;
2 E7 ^4 o' G9 E8 x2 u. w% D - }" q! O$ p$ Z! [& c; i- K) d
- else. K$ W7 c8 a2 j- C- B9 H) d6 S2 t3 I
- {1 o1 B& r; D- o2 J8 _3 i4 { R
- fullUrl = thisUri.ToString();
0 b0 ~+ ^ ]: B8 J& E- e( Y. C( V) d - }, O y( ~% H$ X) t+ J4 A
- allHref.Add(fullUrl);
; Z0 m( m6 g/ R8 w. h3 A - //Console.WriteLine("原链接:" + m.Groups["url"].Value);& t' [. @. `: s0 n+ J
- //Console.WriteLine("文本标记:" + m.Groups["text"].Value);
3 M" B6 ]) C- {4 W - //Console.WriteLine("补全链接:" + fullUrl);
$ v4 ~. H# `% l4 i - //Console.WriteLine("…………………………………………");# e- Q; V6 b' {+ ?3 g2 P* @
- }" S& t- C" e$ |( @% |% X% S
- }" \4 ~; x1 b0 H* P
- }
5 C) q4 `$ b; R. Q$ b) S9 C$ d - catch (Exception ex)
3 d8 U$ g: J5 X$ I; ? - { }: s: m1 R, Y% L! d- y, _# H) P
- return allHref;2 K, t7 B7 ~) T$ ~. @
- }1 L. j. A5 e0 [' B( V* C
- #endregion
复制代码 4 r" s! y4 p+ I) W' y1 i
$ E( }( l; i7 B8 I: O2 K/ c
0 G/ c% d5 u, l, _ |" R- , r3 C/ r+ c5 c5 J2 n6 y
- ; |# g! F o$ A4 N! R, j+ v
- #region 数据去重, [% L7 N% R6 R3 w4 ]+ A
- /// <summary>" ~3 e7 H. k7 B" w, S6 e: c+ y
- /// List<string>去重8 D4 h8 X% N6 j0 S4 @
- /// </summary>% e& l# q( L& B3 |0 [' V) t. ~- L
- /// <param name="list"></param>
, C6 k. _ b) r0 B; A; x4 L - /// <returns></returns>" k2 `6 v. U* `/ ` a# Y& R; K
- public static List<string> getUnqueList(List<string> list)
2 Z4 X" R" p, d/ g' U7 _8 v - {5 S) Z0 m7 g, b& c# Z r8 e
- List<string> list1 = new List<string>();% `" @3 B0 U- ?7 h9 \
- Hashtable hash = new Hashtable();
' ~' S. i, k. O! X - foreach (string s in list)! q& O( v5 G" {* b$ z
- {
( B1 u, _% ]6 d2 X0 _ R* V - if (!hash.ContainsKey(s))+ ~' X; U) d3 L* V9 B; W
- {+ S) l1 m( Z& u+ P0 `/ Q! c# z8 i
- hash.Add(s, s);
5 }( j( ~/ j4 X4 Z( R) u - list1.Add(s);9 |6 V2 P1 X9 F8 N+ [( C
- }7 E1 ?1 G! A# B5 [ E
- }/ x- \" s. ?0 P3 s, b. a
- hash.Clear();" w" k% u0 I5 `: `4 k( l# E
- hash = null;3 K; b( t2 O9 L3 [3 T0 |5 ^
- return list1;
# R& n- C3 s" T% J% s7 p - }0 H1 L1 q8 o% ^3 g- n: K2 n
- #endregion
复制代码
( Z+ l' Z9 J: j/ e. w, e, X o4 s( X! R( @
|
|