日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用JAVA爬取博客里面的所有文章

發(fā)布時(shí)間:2025/3/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用JAVA爬取博客里面的所有文章 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

主要思路:

1、找到列表頁。

2、找到文章頁。

3、用一個(gè)隊(duì)列來保存將要爬取的網(wǎng)頁,爬取隊(duì)頭的url,如果隊(duì)列非空,則一直爬取。

4、如果是列表頁,則抽取里面所有的文章url進(jìn)隊(duì);如果是文章頁,則直接爬取至本地。

?

一個(gè)博客是起始頁url是這樣的:

http://www.cnblogs.com/joyeecheung/

第n頁是這樣的:

http://www.cnblogs.com/joyeecheung/default.html?page=n

文章的url是這樣的:

http://www.cnblogs.com/joyeecheung/p/[0-9]+.html

?

代碼如下:

public class boke {private Queue<String> data = new LinkedList<String>(); //文章頁面String PAGE = "http://www.cnblogs.com/joyeecheung/p/[0-9]+.html";Pattern p = Pattern.compile(PAGE);public void action(String target) throws IOException{Matcher m = p.matcher(target); //如果是文章頁面則讀取if(m.find()){ URL url = new URL(target); HttpURLConnection conn = (HttpURLConnection)url.openConnection();conn.connect();InputStream in = conn.getInputStream();byte[] buf = new byte [1024]; int len = 0;//分割url,把文章的編號(hào)作為文件的名字String [] bufen = target.split("/"); String name = bufen[bufen.length-1]; name = name.replaceAll("html", "txt");File file = new File(name);FileOutputStream fp = new FileOutputStream(file);while((len=in.read(buf))!=-1){ fp.write(buf, 0, len); }fp.close();}//如果是列表頁面//抽取里面的文章頁面連接else{ URL url = new URL(target);HttpURLConnection conn = (HttpURLConnection)url.openConnection();conn.connect(); InputStream in = conn.getInputStream(); byte [] buf = new byte[1024];//把列表頁的內(nèi)容放到ByteArrayOutputStream中ByteArrayOutputStream outStream = new ByteArrayOutputStream();int len = 0;while((len=in.read(buf))!=-1){//System.out.println(len);outStream.write(buf,0,len);}in.close();outStream.close();String content = new String(outStream.toByteArray());Matcher page = p.matcher(content);//抽取文章的urlwhile(page.find()){//將抽取的文章url進(jìn)隊(duì)data.add(page.group());} }}public static void main(String args[]) throws IOException{boke test = new boke();//起始頁面String start = "http://www.cnblogs.com/joyeecheung/";test.data.add(start);//列表頁面String page = "http://www.cnblogs.com/joyeecheung/default.html?page=";//總頁數(shù)int total =15;//將15頁列表頁進(jìn)隊(duì)for(int i=2;i<=total;i++)test.data.add(page+i);//隊(duì)列非空則一直爬取while(!test.data.isEmpty())test.action(test.data.poll()); }}

?

總結(jié)

以上是生活随笔為你收集整理的使用JAVA爬取博客里面的所有文章的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。