1. //趁着有空回头复习了一把正则表达式
    /*
      以下代码以百度某个贴吧的 URL 作为源,实现了读取 EmailAddress 并写入文件保存起来的两个功能,如果要爬取其它信息,可以改写正则实现相应功能
      要点看引入的包可知:
         1.应用到 IO 读写缓冲字符流
         2.应用到正则表达式
         3.URL 对象获取网页信息
         4.util 包的集合框架 ArrayList
    */
    import java.io.*;
  2. import java.util.regex.*;
  3. import java.net.*;
  4. import java.util.*;
  5. class Spider{
  6. public static void main(String [] args) throws Exception{
  7. URL url=new URL("http://tieba.baidu.com/p/2314539885");
  8. //String [] emailAddress=
  9. ArrayList<String> emailList=getEmailByURL(url);
  10. for(String emailAddress:emailList){
  11. System.out.println(emailAddress);
  12. }
  13.  
  14. String qualifiedName="c://users//ghc//desktop//test//emailAddress.txt";
  15.  
  16. writeEmailToFile(qualifiedName,emailList);
  17.  
  18. }
  19. // Read html from url
  20. public static ArrayList<String> getEmailByURL(URL url) throws Exception{
  21. URLConnection urlconn=url.openConnection();
  22. BufferedReader bufreader =new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
  23.  
  24. //regex match pattern
  25. String regex="\\w+@\\w+(\\.\\w+)+";
  26. Pattern p=Pattern.compile(regex);
  27.  
  28. ArrayList<String> emailList=new ArrayList<String>();
  29.  
  30. String line=null;
  31. while((line=bufreader.readLine())!=null){
  32. Matcher m=p.matcher(line);
  33.  
  34. // Start to iterator the one matches
  35.  
  36. while(m.find()){
  37. emailList.add(m.group());
  38. //System.out.println(m.group());
  39. }
  40.  
  41. //System.out.println(line);
  42. }
  43. return emailList;
  44. }
  45. public static void writeEmailToFile(String qualifiedName,ArrayList<String> emailList) throws Exception{
  46. BufferedWriter bufwriter=new BufferedWriter(new FileWriter(qualifiedName));
  47. for(int i=0;i<emailList.size();i++){
  48. bufwriter.write(emailList.get(i));
  49. bufwriter.newLine();
  50. bufwriter.flush();
  51. }
  52. }
  53. }
  54.  
  1. /*读取键盘输入的 三种形式 */
  2. import java.io.*;
  3. import java.util.*;
  4. import java.util.Scanner;
  5. class MyTest{
  6. public static void main(String [] args) throws Exception{
  7. //方法一
  8. Scanner scanner =new Scanner(System.in);
  9. String inputStr=scanner.nextLine();
  10. System.out.println(inputStr);
  11.  
  12. //方法二
  13. BufferedReader bufreader=new BufferedReader(new InputStreamReader(System.in));
  14. String line=null;
  15. while((line=bufreader.readLine())!=null){
  16. System.out.println(line);
  17. }
  18.  
  19. }
  20. }
  21. /* 把叠词 简化 */
  22. class AbrreviateDemo{
  23. public static void main(String [] args){
  24. String str="II...LLL...ove..ee.....you!";
  25.  
  26. String regex="\\.+";
  27. String replaceStr="";
  28. str=retriveStr(str,regex,replaceStr);
  29.  
  30. regex="(.)\\1+";
  31. replaceStr="$1";
  32. str=retriveStr(str,regex,replaceStr);
  33.  
  34. System.out.println(str);
  35. }
  36. public static String retriveStr(String str,String regex,String replaceStr){
  37. return str.replaceAll(regex,replaceStr);
  38. }
  39. }
  40.  
  41. /* 将一堆杂乱的 IP 地址进行排序 */
  42. import java.util.*;
  43. class SortIP{
  44. public static void main(String [] args){
  45. String IP="192.168.0.5 2.2.3.4 127.0.0.1";
  46. printAfterSort(IP);
  47. }
  48. public static void printAfterSort(String str){
  49. String regex="(0*\\d+)";
  50. str=str.replaceAll(regex,"00$1");
  51.  
  52. regex="0*(\\d{3})";
  53. str=str.replaceAll(regex,"$1");
  54.  
  55. System.out.println(str);
  56.  
  57. regex=" +";
  58. String [] strArray=str.split(regex);
  59. Arrays.sort(strArray);
  60.  
  61. for(int i=0;i<strArray.length;i++){
  62. System.out.println(strArray[i].replaceAll("0*(\\d+)","$1"));
  63. }
  64. //System.out.println(str);
  65. }
  66. }
  67.  
  68. /* 邮箱地址校验 */
  69. class checkMailDemo{
  70. public static void main(String [] args){
  71. String str="liyu@gchchina.com.cn";
  72. System.out.println("result: "+checkMail(str));
  73. //String regex="";
  74. }
  75. public static boolean checkMail(String str){
  76. String regex="[a-zA-Z0-9_]+[@][a-zA-Z0-9]+(\\.[a-zA-Z]+){1,3}";
  77. regex="\\w+@\\w+(\\.\\w+){1,3}";
  78. return str.matches(regex);
  79. }
  80. }
  81. /* 从一堆杂乱的字符串中获取需要的手机号码 */
  82. import java.util.regex.*;
  83. class RegexDemo{
  84. public static void main(String [] args){
  85. String str="1afasdf13874057617weojfjlj";
  86. String regex="[1-9][3,5,8]\\d{9}";
  87.  
  88. retriveStr(str,regex);
  89. }
  90. public static void retriveStr(String str,String regex){
  91. Pattern p=Pattern.compile(regex);
  92. Matcher m=p.matcher(str);
  93. while(m.find()){
  94. String tempstr=m.group();
  95. System.out.println(tempstr);
  96. }
  97.  
  98. }
  99. }
  100.  
  101. /* 读取键盘标准输入流并大写方式打印到控制台 */
  102. import java.io.*;
  103. import java.util.*;
  104. class UpercaseSystemIn{
  105. public static void main(String [] args) throws IOException{
  106. InputStream in=System.in;
  107. doUpcaseReadIn(in);
  108. }
  109. public static void doUpcaseReadIn(InputStream in) throws IOException{
  110. BufferedReader bufr=new BufferedReader(new InputStreamReader(in));
  111. String str=null;
  112. while((str=bufr.readLine())!=null){
  113. System.out.println(str.toUpperCase());
  114. if(str.equalsIgnoreCase("exit")) System.exit(0); //break
  115. }
  116. }
  117. }
  118.  
  119. /* 读取某个贴吧邮箱地址并打印到控制台 注意这里的正则*/
  120. import java.net.*;
  121. import java.io.*;
  122. import java.util.*;
  123. import java.util.regex.*;
  124. class SpiderTest{
  125. public static void main(String [] args) throws Exception{
  126. URL url=new URL("http://tieba.baidu.com/p/2314539885");
  127. getEmailAddressFromURL(url);
  128. }
  129. public static void getEmailAddressFromURL(URL url) throws Exception{
  130. URLConnection urlconn=url.openConnection();
  131.  
  132. BufferedReader bufreader = new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
  133.  
  134. String line=null;
  135.  
  136. String regex="\\w+@\\w+(\\.\\w+)+";
  137.  
  138. Pattern p=Pattern.compile(regex);
  139.  
  140. while ((line=bufreader.readLine())!=null){
  141. Matcher m = p.matcher(line);
  142. while(m.find()){
  143. System.out.println(m.group());
  144. }
  145. // System.out.println(line);
  146. }
  147. }
  148. }
  149.  
  150. /* 实现本地二进制文件拷贝 */
  151.  
  152. import java.io.*;
  153. class CopyImg{
  154. public static void main(String [] args){
  155. BufferedInputStream bufinps=null;
  156. BufferedOutputStream bufotps=null;
  157. try{
  158. bufinps=new BufferedInputStream(new FileInputStream("psb.jpg"));
  159. bufotps=new BufferedOutputStream(new FileOutputStream("psb_copy.jpg"));
  160. byte [] buf=new byte[8192];
  161. int len=0;
  162. while((len=bufinps.read(buf))>0){
  163. bufotps.write(buf,0,len);
  164. //bufotps.flush();
  165. }
  166. }
  167. catch(IOException ioe){
  168. ioe.printStackTrace();
  169. }
  170. finally{
  171. if(bufinps!=null)
  172. try{
  173. bufinps.close();
  174. }
  175. catch(IOException ioe){
  176. ioe.printStackTrace();
  177. }
  178. if(bufotps!=null)
  179. try{
  180. bufotps.close();
  181. }
  182. catch(IOException ioe){
  183. ioe.printStackTrace();
  184. }
  185. }
  186. }
  187. }
  188.  
  189. /* 从某个网页爬取符合规则的邮箱地址并保存到本地磁盘路径下 */
  190.  
  191. import java.io.*;
  192. import java.util.regex.*;
  193. import java.net.*;
  194. import java.util.*;
  195. class Spider{
  196. public static void main(String [] args) throws Exception{
  197. URL url=new URL("http://tieba.baidu.com/p/2314539885");
  198. //String [] emailAddress=
  199. ArrayList<String> emailList=getEmailByURL(url);
  200. for(String emailAddress:emailList){
  201. System.out.println(emailAddress);
  202. }
  203.  
  204. String qualifiedName="c://users//ghc//desktop//test//emailAddress.txt";
  205.  
  206. writeEmailToFile(qualifiedName,emailList);
  207.  
  208. }
  209. // Read html from url
  210. public static ArrayList<String> getEmailByURL(URL url) throws Exception{
  211. URLConnection urlconn=url.openConnection();
  212. BufferedReader bufreader =new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
  213.  
  214. //regex match pattern
  215. String regex="\\w+@\\w+(\\.\\w+)+";
  216. Pattern p=Pattern.compile(regex);
  217.  
  218. ArrayList<String> emailList=new ArrayList<String>();
  219.  
  220. String line=null;
  221. while((line=bufreader.readLine())!=null){
  222. Matcher m=p.matcher(line);
  223.  
  224. // Start to iterator the one matches
  225.  
  226. while(m.find()){
  227. emailList.add(m.group());
  228. //System.out.println(m.group());
  229. }
  230.  
  231. //System.out.println(line);
  232. }
  233. return emailList;
  234. }
  235. public static void writeEmailToFile(String qualifiedName,ArrayList<String> emailList) throws Exception{
  236. BufferedWriter bufwriter=new BufferedWriter(new FileWriter(qualifiedName));
  237. for(int i=0;i<emailList.size();i++){
  238. bufwriter.write(emailList.get(i));
  239. bufwriter.newLine();
  240. bufwriter.flush();
  241. }
  242. }
  243. }
  244.  
  245. /* 从某个网页爬取图片的 URL 地址然后 进行 下载到本地磁盘路径 基本功能已经实现,但是正则需要自行调整 */
  246.  
  247. import java.net.*;
  248. import java.io.*;
  249. import java.util.regex.*;
  250. import java.util.*;
  251. class ImgSpider{
  252. public static void main(String [] args){
  253. saveImgFromURL("http://image.baidu.com/","c:/users/ghc/desktop/test/");
  254. System.gc();
  255. }
  256. public static boolean downLoadImg(String line,String path){
  257. boolean flag=true;
  258. FileOutputStream fos=null;
  259. BufferedInputStream bufinpts=null;
  260. BufferedOutputStream bufopts=null;
  261. path=path.replace("<","");
  262. /* System.out.println(line);
  263. System.out.println(path); */
  264. try{
  265. bufinpts=new BufferedInputStream((new URL(line)).openConnection().getInputStream());
  266. fos=new FileOutputStream(path);
  267. bufopts=new BufferedOutputStream(fos);
  268.  
  269. byte [] buf=new byte[1024];
  270.  
  271. int len=-1;
  272. while((len=bufinpts.read(buf))!=-1){
  273. bufopts.write(buf,0,buf.length);
  274. }
  275. }
  276. catch(IOException ioe){
  277. ioe.printStackTrace();
  278. flag=false;
  279. }
  280. finally{
  281. if(bufopts!=null)
  282. try{
  283. bufopts=null;
  284. bufopts.close();
  285. }
  286. catch(IOException ioe){
  287. ioe.printStackTrace();
  288. }
  289. if(fos!=null)
  290. try{
  291. fos=null;
  292. fos.close();
  293. }
  294. catch(IOException ioe){
  295. ioe.printStackTrace();
  296. }
  297.  
  298. }
  299. return flag;
  300. }
  301.  
  302. public static boolean saveImgFromURL(String urlStr,String folder){
  303. boolean flag=true;
  304. URL url=null;
  305.  
  306. //InputStream in=null;
  307. String line=null;
  308. BufferedReader bufr=null;
  309. Pattern p=null;
  310. Matcher m=null;
  311. ArrayList<String> imgList=null;
  312. try{
  313. url=new URL(urlStr);
  314. URLConnection urlconn=url.openConnection();
  315. bufr=new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
  316. imgList=new ArrayList<String>();
  317. String regex="<img.*src=(.*?)[^>]*?>";
  318. p=Pattern.compile(regex);
  319. while((line=bufr.readLine())!=null){
  320. m=p.matcher(line);
  321. while(m.find()){
  322. System.out.println(m.group());
  323. imgList.add(m.group());
  324. }
  325. //System.out.println(line);
  326. }
  327. Iterator<String> it=imgList.iterator();
  328. while(it.hasNext()){
  329. line=it.next();
  330. folder+=line.substring(line.lastIndexOf("/",2) + 1,
  331. 3)+".png";
  332. //http://www.jb51.net/images/logo.gif
  333. m=Pattern.compile("http://(\\w+\\.)+[a-z]+/images/(\\w+\\.)+[a-z]{3}").matcher(line);
  334. while(m.find()){
  335. line=m.group();
  336. //System.out.println(line);
  337. downLoadImg(line,folder);
  338. }
  339. }
  340.  
  341. }
  342. catch(MalformedURLException mfe){
  343. mfe.printStackTrace();
  344. flag=false;
  345. }
  346. catch(IOException ioe){
  347. ioe.printStackTrace();
  348. flag=false;
  349. }
  350.  
  351. finally{
  352. if (bufr!=null)
  353. try{
  354. bufr=null;
  355. bufr.close();
  356. }
  357. catch(IOException ie){ ie.printStackTrace();
  358.  
  359. }
  360.  
  361. }
  362.  
  363. return flag;
  364. }
  365. }
  366.  
  367. /*正则 小练习 */
  368.  
  369. class Demo{
  370. public static void main(String [] args){
  371. String qq="1212345";
  372. boolean checkResult=checkQQ(qq);
  373. System.out.println(checkResult ? qq+" is right": qq+" is wrong!!!");
  374.  
  375. String telnumber="15974097817";
  376. checkResult=checkTel(telnumber);
  377. System.out.println(checkResult ? telnumber+" is right": telnumber+" is wrong!!!");
  378.  
  379. String path="c:\\users\\frank\\abqqcdkkkefghhijkkkkkl.txt";
  380.  
  381. String regex="(.)\\1+"; //叠词切割注意引入组的概念,\n 代表引用第几组 + 出现1次或多次 qq 或者 kkk 均会被当作切割符
  382.  
  383. printAfterSplit(path,regex);
  384.  
  385. System.out.println("=================");
  386.  
  387. regex="\\.";
  388.  
  389. printAfterSplit(path,regex);
  390.  
  391. String str="abcddeffffg";
  392. regex="(.)\\1{3,}";
  393. String replaceStr="$1";
  394. printAfterReplaceStr(str,regex,replaceStr);
  395.  
  396. }
  397. // 以下两个均是正则匹配 校验字符串的函数
  398. public static boolean checkTel(String telnumber){
  399. String regex="[1][3,5,8]\\d{9}";
  400. return telnumber.matches(regex);
  401. }
  402. public static boolean checkQQ(String qq){
  403. //boolean result=false;
  404. String regex="[1-9][0-9]{4,14}";
  405. regex="[1-9]\\d{4,14}";
  406. return qq.matches(regex);
  407. /* int len=qq.length();
  408. if(len<5 || len>15 || qq.startsWith("0")){
  409. System.out.println("length or startWith issue!!!");
  410. //return result;
  411. }
  412. else {
  413. char [] ary=qq.toCharArray();
  414. for(int i=0;i<ary.length;i++){
  415. if(!(ary[i]>='0' && ary[i]<='9'))
  416. {
  417. System.out.println("not between 0 and 9 !!!");
  418. break;
  419. //return result;
  420. }
  421. else
  422. result=true;
  423.  
  424. }
  425. } */
  426. //return result;
  427. }
  428. // 以下两个均是正则 切割字符串的函数
  429. public static void printAfterSplit(String path,String regex){
  430.  
  431. String [] ary=path.split(regex);
  432. for(String s:ary){
  433. System.out.println(s);
  434. }
  435. }
  436. // 以下两个均是自定义正则替换字符串函数
  437. public static void printAfterReplaceStr(String str,String regex,String replaceStr){
  438. String resultStr=str.replaceAll(regex,replaceStr);
  439. System.out.println(resultStr);
  440. }
  441. }

  

  

一个java版本的简单邮箱小爬虫的更多相关文章

  1. java入门---简介&简单输出小例子&开发前准备

        Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称.由James Gosling和同事们共同研发,并在1995年正式推出.J ...

  2. Python 学习(1) 简单的小爬虫

    最近抽空学了两天的Python,基础知识都看完了,正好想申请个联通日租卡,就花了2小时写了个小爬虫,爬一下联通日租卡的申请页面,看有没有好记一点的手机号~   人工挑眼都挑花了. 用的IDE是PyCh ...

  3. 用Nodejs做一个简单的小爬虫

    Nodejs将JavaScript语言带到了服务器端,作为js主力用户的前端们,因此获得了服务器端的开发能力,但除了用express搭建一个博客外,还有什么好玩的项目可以做呢?不如就做一个网络爬虫吧. ...

  4. 利用python写一个简单的小爬虫 爬虫日记(1)(好好学习)

    打开py的IDLE >>>import urllib.request >>>a=urllib.request.urlopen("http://www.ba ...

  5. 一个java实现的简单的4则运算器

    有些基础知识有欠缺,补一下,顺便练习一下java import com.sun.deploy.util.ArrayUtil; import java.util.*; public class Main ...

  6. Python练习,网络小爬虫(初级)

    最近还在看Python版的rcnn代码,附带练习Python编程写一个小的网络爬虫程序. 抓取网页的过程其实和读者平时使用IE浏览器浏览网页的道理是一样的.比如说你在浏览器的地址栏中输入    www ...

  7. JGibbLDA:java版本的LDA(Latent Dirichlet Allocation)实现、修改及使用

    转载自:http://blog.csdn.net/memray/article/details/16810763   一.概述 JGibbLDA是一个java版本的LDA(Latent Dirichl ...

  8. Java实现一个简单的网络爬虫

    Java实现一个简单的网络爬虫 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWri ...

  9. Java豆瓣电影爬虫——小爬虫成长记(附源码)

    以前也用过爬虫,比如使用nutch爬取指定种子,基于爬到的数据做搜索,还大致看过一些源码.当然,nutch对于爬虫考虑的是十分全面和细致的.每当看到屏幕上唰唰过去的爬取到的网页信息以及处理信息的时候, ...

随机推荐

  1. Boyer and Moore Fast majority vote algorithm(快速选举算法)

    问题来来自于leetcode上的一道题目,https://leetcode.com/problems/majority-element/,大意是是找出一个数组中,出现次数超过一个半的数字,要求是O(n ...

  2. 开发工具之Spark程序开发详解

    一  使用IDEA开发Spark程序 1.打开IDEA的官网地址,地址如下:http://www.jetbrains.com/idea/ 2.点击DOWNLOAD,按照自己的需求下载安装,我们用免费版 ...

  3. LintCode——合并排序数组II

    描述:合并两个排序的整数数组A和B变成一个新的数组 样例:给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 1.Python:先将数组B加到数组A之后,然后 ...

  4. abcdocker 的博客

    技术参考总结 abcdocker 的博客 09月 3 篇 20日 Centos7 图形化创建KVM 10日 Nginx 代理Google 进行*** 10日 mac 安装装逼神器cmatrix 08月 ...

  5. java实验报告五

    一.实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 二.实验基础: IP和端口:IP是用来标示计算机,而端口是用来标示某个计算机上面的特定应用.至于它们的 ...

  6. code review & github

    code review & github code review https://github.com/features/code-review/ https://github.com/mar ...

  7. office2013 激活方式

    1.下载   KMSpico_setup 2.关闭所有杀毒 3.打开  KMSpico_setup.exe 安装,下一步下一步,完成 4.打开word2013看下还有没弹出过期,没有即成功 5.卸载k ...

  8. 【题解】 bzoj4004: [JLOI2015]装备购买 (线性基)

    bzoj4004,戳我戳我 Solution: 裸的线性基,这没啥好说的,我们说说有意思的地方(就是我老是wa的地方) Attention: 这题在\(luogu\),上貌似不卡精度,\(bzoj\) ...

  9. 【BZOJ4883】棋盘上的守卫(最小生成树)

    [BZOJ4883]棋盘上的守卫(最小生成树) 题面 BZOJ 题解 首先\(n\)行\(m\)列的棋盘显然把行列拆开考虑,即构成了一个\(n+m\)个点的图.我们把格子看成边,那么点\((x,y)\ ...

  10. 从web启动winform程序

    最近有个客户提出想从网站上启动一个客户端的程序,研究了下,实现方法如下: 1. 注入注册表 try                {                    string appPath ...