Java Writing web crawler notes (Part III: Jsoup's Power)

Source: Internet
Author: User

Based on the HttpClient download page, followed by the URL should be extracted, the first I used is htmlpraser, after a few days, I found that there are jsoup this package, very useful, and then I will directly use Jsoup To crawl the page and extract the URL inside, here to share the code with you.

Import Java. IO. IOException;Import Java. Util. HashSet;Import Java. Util. Iterator;Import Java. Util. Set;import org. Jsoup. Jsoup;import org. Jsoup. Nodes. Document;import org. Jsoup. Nodes. Element;import org. Jsoup. Select. Elements;public class Jsoup {public staticSet<String> extractlinks (String URL) {Set<String> urls = new HashSet ();try {Document doc =jsoup. Connect(URL). Get();Elements Links=doc. Select("A[href]");System. out. println(Links. Size());for (Element link:links) {String Url =link. attr("Abs:href");URLs. Add(URL);}} catch (IOException e) {//TODO auto-generated catch block E. Printstacktrace();} return URLs;}

Java Writing web crawler notes (Part III: Jsoup's Power)

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.