Tip: how to get all the-mark herf link code of a web page

Source: Internet
Author: User

Using System. IO;
Using System. text;
Using System. Text. regularexpressions;
Using System. net;

1. Obtain the original webpageCode

Uri URL = New Uri ( " Http://www.blogjava.net/wujun " );
Httpwebrequest request = (Httpwebrequest) webrequest. Create (URL );
Httpwebresponse response = (Httpwebresponse) request. getresponse ();
Stream stream = Response. getresponsestream ();
Streamreader SR =   New Streamreader (Stream );
String Str = Sr. readtoend ();
Sr. Close ();
Stream. Close ();
Response. Close ();

Obtain the HTML of the webpageSource codeLater. Analyze all <a href = "url"> according to the source code, and finally obtain the URL of the URL after href.

Regular Expression RegEx regexfindhref =   New RegEx ( @" <A \ s + ([^>] * \ s *)? Href \ s * = \ s *(? :""(? <1> [/\ a-z0-9 _] [^ ""] *) "" | '(? <1> [/\ a-z0-9 _] [^ '] *)'
| (? <1> [/\ a-z0-9 _] \ s *) (\ s [^>] *)?> (? <2> .*?) </A> " , Regexoptions. singleline | Regexoptions. ignorecase | Regexoptions. Compiled );

Loop read connection address

For (Match m = Regexfindhref. Match (STR); M. success; m = M. nextmatch ())
{
Textbox1.text + = M. Groups [1 ]. Tostring () + " \ N " ;

}

After running
Textbox1 displays connections to all webpages after analysis:

http://www.dotlucene.net/
http://www.castleproject.org/
http://www.codeplex.com/
http://www.codeproject.com/
http://www.asp.net/
http://www.nhibernate.org/
http://www.blogjava.net/wujun/CommentsRSS.aspx
http://www.blogjava.net/wujun/archive/2006/10/23/47150.html#76745
> http://www.blogjava.net/wujun/archive/2006/10/23.html
http://www.blogjava.net/wujun/archive/2006/10/23 /76769.html
http://www.blogjava.net/wujun/archive/2006/10/23/76769.html
http://www.blogjava.net/wujun/archive/2006/10/23/76769.html#FeedBack
http://www.blogjava.net/wujun/admin/EditPosts.aspx? Postid = 76769
http://www.blogjava.net/wujun/AddToFavorite.aspx? Id = 76769
http://www.blogjava.net/wujun/archive/2006/10/20.html
......
..............
......................... and so on...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.