中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

利用htmlunit下載網(wǎng)頁上的文件

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import org.apache.commons.io.IOUtils;
 
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
 
public class DownloadFile {
    public static void main(String[] args) throws Exception {
        String baseUrl = "<a  target="_blank">http://hanyu.iciba.com/hanzi/1.shtml";</a>
        String bihuaRegex = "class=\"guanggao\"[^<]*<[^<]*<param\\s*name=\"movie\"\\s*value=\"([^\"]*)";
        String aSoundRegex = "class=\"js12\">ā.*?name=\"FlashVars\"\\s*value=\"f=([^\"]*)";
        String eSoundRegex = "class=\"js12\">ē.*?name=\"FlashVars\"\\s*value=\"f=([^\"]*)";
        WebClient client = new WebClient();
        client.getOptions().setCssEnabled(false);
        client.getOptions().setJavaScriptEnabled(false);
        client.getOptions().setThrowExceptionOnFailingStatusCode(false);
        client.getOptions().setThrowExceptionOnScriptError(false);
        Page page = client.getPage(baseUrl);
        String source = page.getWebResponse().getContentAsString();
        Matcher mBihuan = Regex(source, bihuaRegex);
        Matcher mA = Regex(source, aSoundRegex);
        Matcher mE = Regex(source, eSoundRegex);
        while(mBihuan.find()) {
            String url = "<a  + mBihuan.group" target="_blank">http://hanyu.iciba.com/" + mBihuan.group</a>(1);
            page = client.getPage(url);
            saveFile(page, "d:/testDownload/bihua.swf");
        }
        while(mA.find()) {
            String url = mA.group(1);
            page = client.getPage(url);
            saveFile(page, "d:/testDownload/a.mp3");
        }
        while(mE.find()) {
            String url = mE.group(1);
            page = client.getPage(url);
            saveFile(page, "d:/testDownload/e.mp3");
        }
    }
     
    public static Matcher Regex(String source, String regex) {
        Pattern p = Pattern.compile(regex, Pattern.DOTALL);
        return p.matcher(source);
    }
     
    public static void saveFile(Page page, String file) throws Exception {
        InputStream is = page.getWebResponse().getContentAsStream();
        FileOutputStream output = new FileOutputStream(file);
        IOUtils.copy(is, output);
        output.close();
    }
}

標簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:php實現(xiàn)文件下載!

下一篇:Java6實現(xiàn)調(diào)用操作平臺桌面系統(tǒng)