android 封裝抓取網(wǎng)頁信息的實(shí)例代碼
更新時(shí)間:2013年06月09日 15:49:36 作者:
android 封裝抓取網(wǎng)頁信息的實(shí)例代碼,需要的朋友可以參考一下
復(fù)制代碼 代碼如下:
package cn.mypic;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GetContentPicture {
//得到了圖片地址并下載圖片
public void getHtmlPicture(String httpUrl) {
URL url;
BufferedInputStream in;
FileOutputStream file;
int count; //圖片文件名序號
FileNumber num=new FileNumber();//圖片文件名序號類,num為對象
count=num.NumberReadFromFile();//獲取圖片文件序號
try {
System.out.println("獲取網(wǎng)絡(luò)圖片");
String fileName = (String.valueOf(count)).concat(httpUrl.substring(httpUrl.lastIndexOf(".")));//圖片文件序號加上圖片的后綴名,后綴名用了String內(nèi)的一個(gè)方法來獲得
//httpUrl.substring(httpUrl.lastIndexOf("/"));//這樣獲得的文件名即是圖片鏈接里圖片的名字
String filePath = "d:/image/";//圖片存儲的位置
url = new URL(httpUrl);
in = new BufferedInputStream(url.openStream());
file = new FileOutputStream(new File(filePath+fileName));
int t;
while ((t = in.read()) != -1) {
file.write(t);
}
file.close();
in.close();
System.out.println("圖片獲取成功");
count=count+1;//圖片文件序號加1
num.NumberWriteToFile(count);//將圖片名序號保存
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//獲取網(wǎng)頁的代碼保存在String格式的Content中
public String getHtmlCode(String httpUrl) throws IOException {
String content ="";
URL uu = new URL(httpUrl); // 創(chuàng)建URL類對象
BufferedReader ii = new BufferedReader(new InputStreamReader(uu
.openStream())); // //使用openStream得到一輸入流并由此構(gòu)造一個(gè)BufferedReader對象
String input;
while ((input = ii.readLine()) != null) { // 建立讀取循環(huán),并判斷是否有讀取值
content += input;
}
ii.close();
return content;
}
//分析網(wǎng)頁代碼,找到匹配的網(wǎng)頁圖片地址
public void get(String url) throws IOException {
String searchImgReg = "(?x)(src|SRC|background|BACKGROUND)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";//用于在網(wǎng)頁代碼Content中查找匹配的圖片鏈接。
String searchImgReg2 = "(?x)(src|SRC|background|BACKGROUND)=('|\")(http://([\\w-]+\\.)+[\\w-]+(:[0-9]+)*(/[\\w-]+)*(/[\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";
String content = this.getHtmlCode(url);//this指對象gcp,在此地調(diào)用獲取網(wǎng)頁代碼,getHtmlCode方法
//System.out.println(content); //輸出的content將是一個(gè)連續(xù)的字符串。
Pattern pattern = Pattern.compile(searchImgReg);//java.util.regex.Pattern
Matcher matcher = pattern.matcher(content); //java.util.regex.Matcher
while (matcher.find()) {
System.out.println(matcher.group(3));//輸出圖片鏈接地址到屏幕
// System.out.println(url);
this.getHtmlPicture(matcher.group(3));//對象調(diào)用getHtmlPicture從網(wǎng)上下載并輸出圖片文件到指定目錄
}
pattern = Pattern.compile(searchImgReg2);
matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println(matcher.group(3));
this.getHtmlPicture(matcher.group(3));
}
// searchImgReg =
// "(?x)(src|SRC|background|BACKGROUND)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";
}
//主函數(shù)url網(wǎng)頁的地址
public static void main(String[] args) throws IOException {
String url = "http://www.baidu.com";
GetContentPicture gcp = new GetContentPicture();
gcp.get(url);
}
}
復(fù)制代碼 代碼如下:
package cn.mypic;
import java.io.*;
public class FileNumber{
//文件寫
public void NumberWriteToFile(int x){
int c=0;
c=x;
File filePath=new File("d:/image");//文件名序號TXT文件保存地址
File f1=new File(filePath,"number.txt");
try{
FileOutputStream fout=new FileOutputStream(f1);
DataOutputStream out=new DataOutputStream(fout);
out.writeInt(c);
}
catch(FileNotFoundException e){
System.err.println(e);
}
catch(IOException e){
System.err.println(e);
}
}
//文件讀
public int NumberReadFromFile(){
int c1 = 0;
File filePath=new File("d:/image");
File f1=new File(filePath,"number.txt");
try{
FileInputStream fin=new FileInputStream(f1);
DataInputStream in=new DataInputStream(fin);
c1=in.readInt();
System.out.println(c1);//輸出文件內(nèi)容至屏幕
}
catch(FileNotFoundException e){
System.err.println(e);
}
catch(IOException e){
System.err.println(e);
}
return c1;
}
public static void main(String args[]){
}
}
相關(guān)文章
Kotlin協(xié)程操作之創(chuàng)建啟動掛起恢復(fù)詳解
本文的定位是協(xié)程的創(chuàng)建、啟動、掛起、恢復(fù),也會示例一些簡單的使用,這里不對suspend講解,,也不對協(xié)程的高級用法做闡述(熱數(shù)據(jù)通道Channel、冷數(shù)據(jù)流Flow...),本文主要講協(xié)程稍微深入的全面知識2022-08-08android6.0運(yùn)行時(shí)權(quán)限完美封裝方法
今天小編就為大家分享一篇android6.0運(yùn)行時(shí)權(quán)限完美封裝方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07基于Android AIDL進(jìn)程間通信接口使用介紹
本篇文章小編為大家介紹,基于Android AIDL進(jìn)程間通信接口使用介紹。需要的朋友參考下2013-04-04Android自定義View實(shí)現(xiàn)葉子飄動旋轉(zhuǎn)效果(四)
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)葉子飄動旋轉(zhuǎn)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android中webView加載H5綁定cookie實(shí)例
這篇文章主要介紹了Android中webView加載H5綁定cookie實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03