1. 程式人生 > >java之 25天 網路爬蟲抓取圖片(二)

java之 25天 網路爬蟲抓取圖片(二)

[size=medium][b]正則表示式練習[/b][/size]


import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;

public class RegexTest {


public static void main(String[] args) {
//test();
ipSort();
}
/**
* 需求:對郵件地址進行校驗
*/
public static void checkMail(){
String mail="[email protected]";
String regex="\\w{6,12}@[a-zA-Z0-9]+(\\.[a-zA-Z]+){1,3}"; //較為精確的匹配,
regex="\\
[email protected]
\\w+(\\.\\w+)+"; //相對不太精確的匹配 註冊後,傳送啟用郵件

System.out.println(mail.matches(regex));
}

/**
* 需求:
* 將蝦類字串轉換成, 我要學程式設計
* 到底用四種功能中的那一個呢,或者哪幾個呢?
* 思路方式:
* 1.如果指向知道該字串是否對是錯,使用匹配.
* 2.想要將已有的字串變成另一個字串,替換
* 3.想要安裝指定的方式將字串變成多個字串,切割. 獲取匹配規則以外的子串
* 4.想要拿到符合要求的字串子串, 獲取. 獲取符合規則的子串.
*/
public static void test(){
String str="我我....我我...我要....要要.....要要...學學學..學學.程式設計....程程...程";

/**
* 將已有字串變成一個字串, 使用 替換功能.
* 1.可以先去掉 " ."
* 2.在將多個充分度的內容變成單個內容.
*/
str=str.replaceAll("\\.+", "");
System.out.println(str);

str=str.replaceAll("(.)\\1+", "$1");
System.out.println(str);

}

/**
* 192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30
* 將IP地址進行地址段順序的排序.
* 還按照字串自然順序,只要讓他們每一段都是3位即可
* 1.按照每一段需要的最少多0進行補齊,那麼每一段就會至少保證有3位
* 2.將每一段只保留3位,這樣,所有的Ip地址都是每一段3位.
*/
public static void ipSort(){
String ip="192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30 127.0.0.1";
ip=ip.replaceAll("(\\d+)", "00$1");
System.out.println(ip);
ip=ip.replaceAll("0*(\\d{3})", "$1");
System.out.println(ip);
String[] arr=ip.split(" +");
//方法一
//Arrays.sort(arr);

//方法二
TreeSet<String> ts=new TreeSet<String>();
for(String s:arr){
ts.add(s);
}
for(String s:ts){
System.out.println(s.replaceAll("0*(\\d+)", "$1"));
//System.out.println(s.replaceAll("0*([1-9]\\d*)", "$1"));
}

//方法三
List<String> list= Arrays.asList(arr);
for (String s : list) {
System.out.println(s);
}
Collections.sort(list);
}


}


[size=medium][b]網路爬蟲:(抓取 萊伊份網站首頁的圖片,然後下載儲存到本地)
抓取本地檔案中的所有郵箱[/b][/size]


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* 網頁爬蟲(蜘蛛)
* 爬郵箱
* 抓取 萊伊份 網站首頁 的圖片 然後儲存的本地 C:\
* 採用多執行緒進行下載
*/
class downloadPic implements Runnable{
private String picpath;
private String path;
downloadPic(String picpath,String dir){
this.picpath=picpath;
this.path=dir;
}
public void run(){
try {
URL url=new URL(picpath.replaceAll(" ", "%20"));
URLConnection conn=url.openConnection();
//設定請求的路徑
//conn.setConnectTimeout(5*1000);
int index=picpath.lastIndexOf("/");
int index1=picpath.lastIndexOf("?");
index1=index1==-1? picpath.length():index1;

File dir=new File(path);
if(!dir.exists())
dir.mkdirs();

String filename=path+picpath.substring(index,index1);

BufferedInputStream bis=new BufferedInputStream(conn.getInputStream());

BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(new File(filename)));

byte[] buf=new byte[1024];
int len=0;
while((len=bis.read(buf))!=-1){
bos.write(buf, 0, len);
bos.flush();
}

bis.close();
bos.close();

} catch (Exception e) {
e.printStackTrace();
}

}
}

/**
* 抓取 圖片
* 抓取 本地檔案中 所有 郵箱
* @author Bin
*
*/
public class RegexTest2 {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//getMails();
getMails_1();
}

public static void cashPicAndMail() throws IOException{

URL url=new URL("http://www.laiyifen.com/");

URLConnection conn= url.openConnection();
BufferedReader bufr=new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));

String line=null;
String mailReg="\\
[email protected]
\\w+(\\.\\w+)+"; //抓取網站中的郵箱正則
mailReg="<img .*? ?src=\"(http:.*?)\".*?( /)?>"; //抓取圖片正則

//http://images3.laiyifen.com/laiyifen/2012/10211/10211_01_s.jpg?1332472469#h

//<img src="http://images.laiyifen.com/themes/laiyifen2/images/wl110.jpg">
//<img border="0" src="http://images.laiyifen.com/themes/laiyifen2/images/tribe_image01.jpg">
//<img style="width:80px; height:80px;overflow:hidden;" src="http://images4.laiyifen.com/laiyifen/2012/10211/10211_01_s.jpg?1332472469#h" />

Pattern p=Pattern.compile(mailReg);

int count=0;
String filedir="c:\\laiyifeng\\";
while((line=bufr.readLine())!=null){
//System.out.println(line);
Matcher m=p.matcher(line);
while(m.find()){
String picurl=m.group(1);
new Thread(new downloadPic(picurl,filedir)).start();
count++;
}
}
System.out.println("總計:"+count);

}



/**
* 獲取指定文件中的郵件地址
* 使用個獲取功能,Pattern Matcher
* @throws IOException
*/
public static void getMails() throws IOException{
BufferedReader bufr=new BufferedReader(new FileReader("E:\\mail.txt"));
String line=null;
String mailReg="\\[email protected]\\w+(\\.\\w+)+";
Pattern p=Pattern.compile(mailReg);

while((line=bufr.readLine())!=null){
Matcher m=p.matcher(line);
while(m.find()){
System.out.println(m.group());
}
}
}
}

相關推薦

java 25 網路爬蟲圖片()

[size=medium][b]正則表示式練習[/b][/size]import java.util.Arrays;import java.util.Collections;import java.util.List;import java.util.TreeSet;publ

python 網路爬蟲圖片

#-*- encoding: utf-8 -*- ''' Created on 2014-4-24 @author: Leon Wong ''' import urllib2 import urllib import re import time import os im

java演算法-網路爬蟲網頁並儲存

從一個URL中讀取網頁,如果是同一個網站的就儲存,URL裡面包含URL列表,繼續抓取,抓完全部 使用多執行緒 A執行緒讀取URL內容 B執行緒存檔案 C執行緒解析URL 發現新URL從A執行緒讀取完的內容可以放到一個佇列裡面,B執行緒來讀取,C執行緒解析URL  問題,如果這個佇列

python網路爬蟲--股票資訊到Mysql

1.建表mysql -u root -p 123456create database test default character set utf8;create table stocks --a股(  code varchar(10) comment '程式碼',  nam

Python網路爬蟲動態網頁並將資料存入資料庫MYSQL

簡述 以下的程式碼是使用python實現的網路爬蟲,抓取動態網頁http://hb.qq.com/baoliao/。此網頁中的最新、精華下面的內容是由JavaScript動態生成的。審查網頁元素與網頁原始碼是不同。 本人對於Python學習建立了一個小小的學習圈子,為各位提供了

搜尋引擎—網路爬蟲策略

爬蟲的不同抓取策略,就是利用不同的方法確定待抓取URL佇列中URL優先順序的。 爬蟲的抓取策略有很多種,但不論方法如何,基本目標一致:優先選擇重要網頁進行抓取。 網頁的重要性,評判標準不同,大部分採用網頁的流行性進行定義。 效果較好或有代表性的抓取策略:

python爬蟲圖片

關於python爬蟲一直以來是很著名的,林林總總也有很多方法,大致起來也就是一個原理。 下面我來介紹一下我用的BeautifulSoup獲取的,正則獲取也很簡單,在這裡只說一下BeautifulSoup方法,使用伯樂線上網站作為參考的例子 程式碼如下 #encoding

java 開發用到網路爬蟲汽車家網站全部資料經歷

經歷了兩個禮拜的折騰,某某知名網站的資料終於到手了。犯罪沒被發現這種心情感覺很爽。 說一下我的犯罪經歷,之前公司總是抓取某某網站資料,可能是被發現了。某某網站改變了策略。通過各種技術終止了我們的行為,導致我們的抓取功能報錯,逐步跟蹤,發現我們之前是在人家的網站,通過Webh

JAVA使用Gecco爬蟲 網頁內容

log pro 指定 get www. error 一個 log4j java類 JAVA 爬蟲工具有挺多的,但是Gecco是一個挺輕量方便的工具。 先上項目結構圖。 這是一個 JAVASE的 MAVEN 項目,要添加包依賴,其他就四個文件。log4j.propertie

[js高手路]Node.js實現簡易的爬蟲-博客所有文章列表信息

r.js 目錄 ref 抓取 {} attr 視頻 json clist 抓取目標:就是我自己的博客:http://www.cnblogs.com/ghostwu/ 需要實現的功能: 抓取博客所有的文章標題,超鏈接,文章摘要,發布時間 需要用到的庫: node.js自帶的h

Python爬蟲使用正則表示式資料

目錄 匹配標籤 匹配title標籤 a標籤 table標籤 匹配標籤裡面的屬性 匹配a標籤裡面的URL 匹配img標籤裡的 src 相關文章:Linux中的正則表示式             &nbs

python網路爬蟲汽車家的最新資訊和照片

實現的功能是爬取汽車之家的最新資訊的連結 題目和文章中的照片 爬蟲需要用到我們使用了 requests 做網路請求,拿到網頁資料再用 BeautifulSoup 進行解析 首先先檢查是否安裝了pip,如果已經安裝了pip,直接pip install requests,pip uninstal

Python爬蟲requests+正則表示式貓眼電影top100以及瓜子二手網二手車資訊(四)

{'index': '1', 'image': 'http://p1.meituan.net/movie/[email protected]_220h_1e_1c', 'title': '霸王別姬', 'actor': '張國榮,張豐毅,鞏俐', 'time': '1993-01-01', 'sc

java 爬蟲 網易雲音樂

大家好,我是烤鴨:      今天和大家交流一下爬蟲,抓取網易雲音樂。只討論技術,不提倡其他的。 1.    找音樂源地址 谷歌瀏覽器 F12 ,找請求型別是 Media的。      2.&nb

java實現簡單的網路爬蟲(爬電影天堂電影資訊)

在最開始,我們要在網上下載所用到的jar包,應為這只是一個簡單的網路爬蟲所以很多包裡的內容沒有用到。 下面幾個包就可以了。並且要引入這些包。 主類Bigdata.javaimport org.htmlparser.util.ParserException; public

程式爬蟲網路有用資源,分享給自學愛好者

作者:西邊人,西說測試程式爬蟲抓取有用資源共享給大家頭條號 傳送、公眾號 傳送 也可以搜尋(軟體測試資源站)關注。關注後,私信回覆【資源包】獲取如下內容,測試資料、測試工具、Python、效率軟體、自動

Jsoup 爬蟲 網路圖片

package common; import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStr

java爬蟲資料,儲存為excel檔案

下載jsoup jar包和poi jar包 City.java package dataToExcel; public class City { private String name; private String url;

關於java實現需要登入且帶驗證碼的定時網路爬蟲(爬的資料存庫)

 博主6月初的時候換了個工作,剛進來的時候什麼事沒有,愣是上班喝茶逛網站渡過了一週。那週週五的boss突然問我會不會爬蟲。 作為一個才工作一年的javaer表示根本沒接觸過,但是那種情況下你還敢說不會麼,但是當時也不敢說的很絕對,因此就和boss就會一點。 當時就隱隱約約有爬

Java爬蟲網頁圖片

                 昨天突然想搞下抓取網上的圖片所以寫了下 import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io