Use Jsoup and multithreading to download Wang Wang English MP3

Source: Internet
Author: User

Import Java.io.ioexception;import org.jsoup.jsoup;import org.jsoup.nodes.document;import org.jsoup.nodes.Element; Import Org.jsoup.select.elements;public class Snippet {public static void main (string[] args) throws ioexception{string B                Aseurl = "http://www.wwenglish.com";                 Document Listdoc = Jsoup.connect ("http://www.wwenglish.com/en/club/cnntv/"). get ();                Elements dd = listdoc.select ("td.category td.category_main a.list");        for (Element element:dd) {String Mp3pageurl = baseUrl + element.attr ("href");            System.out.println (Mp3pageurl);            Document Mp3doc = Jsoup.connect (Mp3pageurl). get ();            Elements mp3elements = Mp3doc.select ("A.wwmp3"); if (null! = mp3elements && mp3elements.size () >0) {String Mp3url = baseUrl + mp3elements.get (0). at                TR ("href");            String fileName = mp3url.substring (Mp3url.lastindexof ("/"));            System.out.println (FileName); DownLoad thread = new DownLoad (Mp3url, "F:/wwenglish" +filename);            Thread.Start (); }        }}}


Import Java.io.bufferedoutputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.outputstream;import Java.net.url;public class DownLoad extends Thread {public DownLoad (String mp3url, string filepath) {//Open a URL streamtry {URL url = new URL (mp3url); InputStream in = Url.openstre AM (); OutputStream out = new Bufferedoutputstream (new FileOutputStream (filepath)), for (int b; (b = In.read ())! =-1;) {Out.write (b);} Out.close (); In.close ();} catch (IOException e) {e.printstacktrace ();}}}

Implemented the following link described in the function, the original code is not complete. There is no use of HttpClient, and the use of Jsoup alone achieves the goal.

Simple to use httpclient jsoup to do the extraction of Wang Wang English


The library relies on the following:

<?xml version= "1.0" encoding= "UTF-8"? ><classpath><classpathentry kind= "src" path= "src"/>< Classpathentry kind= "Con" path= "org.eclipse.jdt.launching.jre_container/ org.eclipse.jdt.internal.debug.ui.launcher.standardvmtype/javase-1.8 "/><classpathentry kind=" Lib "path=" H :/mavenrepository/org/jsoup/1.8.1/jsoup-1.8.1.jar "/><classpathentry kind=" Lib "path=" H:/MavenRepository/ Org/apache/httpcomponents/httpclient/4.0/httpclient-4.0.jar "/><classpathentry kind=" Lib "path=" H:/ Mavenrepository/org/apache/httpcomponents/httpcore/4.3.2/httpcore-4.3.2.jar "/><classpathentry kind=" Output "path=" Bin "/></classpath>



Use Jsoup and multithreading to download Wang Wang English MP3

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.