android 封裝抓取網頁資訊的執行個體代碼

來源:互聯網
上載者:User

複製代碼 代碼如下: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("擷取網狀圖片");
String fileName = (String.valueOf(count)).concat(httpUrl.substring(httpUrl.lastIndexOf(".")));//圖片檔案序號加片的尾碼名,尾碼名用了String內的一個方法來獲得
//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();
}
}

//擷取網頁的代碼儲存在String格式的Content中
public String getHtmlCode(String httpUrl) throws IOException {
String content ="";
URL uu = new URL(httpUrl); // 建立URL類對象
BufferedReader ii = new BufferedReader(new InputStreamReader(uu
.openStream())); // //使用openStream得到一輸入資料流並由此構造一個BufferedReader對象
String input;
while ((input = ii.readLine()) != null) { // 建立讀取迴圈,並判斷是否有讀取值
content += input;
}
ii.close();
return content;
}
//分析網頁代碼,找到匹配的網頁圖片地址
public void get(String url) throws IOException {

String searchImgReg = "(?x)(src|SRC|background|BACKGROUND)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";//用於在網頁代碼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,在此地調用擷取網頁代碼,getHtmlCode方法
//System.out.println(content); //輸出的content將是一個連續的字串。

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));//對象調用getHtmlPicture從網上下載並輸出圖片檔案到指定目錄

}

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)))('|\")";
}
//主函數url網頁的地址
public static void main(String[] args) throws IOException {

String url = "http://www.baidu.com";
GetContentPicture gcp = new GetContentPicture();
gcp.get(url);

}

}

複製代碼 代碼如下: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);//輸出檔案內容至螢幕
}
catch(FileNotFoundException e){
System.err.println(e);
}
catch(IOException e){
System.err.println(e);
}
return c1;
}
public static void main(String args[]){

}

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.