博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ugg
阅读量:6582 次
发布时间:2019-06-24

本文共 3944 字,大约阅读时间需要 13 分钟。

[html]

  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.net.HttpURLConnection;
  9. import java.net.SocketException;
  10. import java.net.URL;
  11. import java.util.Properties;
  12. public class Test2 {
  13. public static void main(String[] args) {
  14. // getHost("http://www.youdaili.net/Daili/http/2572_4.html") ;
  15. File f = null ;
  16. BufferedReader br = null ;
  17. FileReader fr = null ;
  18. String s = "" ;
  19. String[] host = new String[2] ;
  20. try {
  21. f = new File("E:\\host.txt") ;
  22. fr = new FileReader(f) ;
  23. br = new BufferedReader(fr) ;
  24. s = br.readLine() ;
  25. while(s!=null) {
  26. System.out.println("换一次ip" + s);
  27. host = s.split(":") ;
  28. //******************************
  29. Properties sys = System.getProperties();
  30. sys.put("proxySet","true");
  31. sys.put("proxyHost",host[0]);
  32. sys.put("proxyPort",host[1]);
  33. System.setProperties(sys);
  34. //******************************
  35. getData() ;
  36. s = br.readLine() ;
  37. }
  38. } catch (Exception e) {
  39. System.out.println(e);
  40. }finally {
  41. try {
  42. if(br != null) {
  43. br.close();
  44. }
  45. if(fr != null) {
  46. fr.close();
  47. }
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. }
  53. public static void getData() {
  54. System.out.println("进入getData");
  55. URL url ;
  56. int responseCode ;
  57. HttpURLConnection httpURLConnection ;
  58. String str = "" ;
  59. BufferedReader br = null ;
  60. StringBuffer sb = new StringBuffer() ;
  61. InputStreamReader isr = null ;
  62. try {
  63. for(int i=0;i<200;i++){
  64. url = new URL("http://www.uggaustralia.cn/thisisugg/api.php?ctrl=page&action=voteWeibo&pid=4218") ;
  65. httpURLConnection = (HttpURLConnection)url.openConnection() ;
  66. httpURLConnection.setConnectTimeout(5000);
  67. httpURLConnection.setReadTimeout(5000);
  68. httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11") ;
  69. responseCode = httpURLConnection.getResponseCode() ;
  70. if(responseCode ==200){
  71. isr = new InputStreamReader(httpURLConnection.getInputStream(),"GBK") ;
  72. br = new BufferedReader(isr) ;
  73. while((str=br.readLine())!=null){
  74. sb.append(str) ;
  75. }
  76. }else{
  77. throw new SocketException() ;
  78. }
  79. System.out.println(sb.toString());
  80. if(sb.toString().contains("\"data\":2")) {
  81. break ;
  82. }
  83. sb = new StringBuffer() ;
  84. }
  85. } catch (Exception e) {
  86. System.out.println(e);
  87. }finally {
  88. try {
  89. if(br != null) {
  90. br.close();
  91. }
  92. if(isr != null) {
  93. isr.close();
  94. }
  95. } catch (IOException e) {
  96. e.printStackTrace();
  97. }
  98. }
  99. }
  100. /**
  101. * 获得有代理网上的ip
  102. */
  103. public static void getHost(String dailiURL) {
  104. URL url ;
  105. int responseCode ;
  106. HttpURLConnection httpURLConnection ;
  107. String str = "" ;
  108. BufferedReader br = null ;
  109. StringBuffer sb = new StringBuffer() ;
  110. InputStreamReader isr = null ;
  111. try {
  112. url = new URL(dailiURL) ;
  113. httpURLConnection = (HttpURLConnection)url.openConnection() ;
  114. httpURLConnection.setConnectTimeout(5000);
  115. httpURLConnection.setReadTimeout(5000);
  116. httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11") ;
  117. responseCode = httpURLConnection.getResponseCode() ;
  118. if(responseCode ==200){
  119. isr = new InputStreamReader(httpURLConnection.getInputStream(),"utf-8") ;
  120. br = new BufferedReader(isr) ;
  121. str = br.readLine() ;
  122. while(br.readLine()!=null){
  123. if(str.contains("@HTTP#")){
  124. str = str.replace("<p>", "").trim() ;
  125. str = str.substring(0, str.indexOf("@")) ;
  126. str += "\r\n" ;
  127. sb.append(str) ;
  128. }
  129. str = br.readLine() ;
  130. }
  131. System.out.println(sb.toString());
  132. HostwriteToFile(sb.toString()) ;
  133. }
  134. } catch (Exception e) {
  135. e.printStackTrace();
  136. }finally {
  137. try {
  138. if(br != null) {
  139. br.close();
  140. }
  141. if(isr != null) {
  142. isr.close();
  143. }
  144. } catch (IOException e) {
  145. e.printStackTrace();
  146. }
  147. }
  148. }
  149. /**
  150. * 把拿到的代理ip写入文件中
  151. * @param str
  152. */
  153. public static void HostwriteToFile(String str) {
  154. FileWriter fw = null ;
  155. try {
  156. fw = new FileWriter("E:\\host.txt",true) ;
  157. fw.write(str);
  158. fw.flush();
  159. fw.close();
  160. } catch (IOException e) {
  161. e.printStackTrace();
  162. }
  163. }
  164. }

转载地址:http://fnsno.baihongyu.com/

你可能感兴趣的文章
CSS-选择器6-兄弟选择器
查看>>
吴恩达《机器学习》课程总结(9)神经网络的学习
查看>>
Binary Tree Paths
查看>>
Android开源之仿微信UI
查看>>
大蕉毕业三周年了,有话对你说 No.103
查看>>
Workbox3 - ServiceWorker可以如此简单
查看>>
重温Servlet学习笔记--session对象
查看>>
webpack + vue
查看>>
Android两条并排RecyclerView实时联动滑动增强
查看>>
进程、轻量级进程(LWP)、线程
查看>>
虽获滴滴投资,但我们依旧不看好ofo的未来
查看>>
有人WIFI模块使用详解
查看>>
爱普生AR眼镜应用场景图鉴:八个领域案例都在这里
查看>>
亚马逊于英国正式推出无人机送货,首单仅花13分钟
查看>>
Jenkins使用jenkins-cli.jar进行远程调用时出现“ERROR: No such job 'test'”或者权限不够等问题解决(Windows)...
查看>>
VMware给虚拟机绑定物理网卡
查看>>
研究人员发现火星存在大量水冰积层,且距离地表只有几米
查看>>
[转]两种高性能I/O设计模式(Reactor/Proactor)的比较
查看>>
使用函数计算对表格存储中数据做简单清洗
查看>>
《Scikit-Learn与TensorFlow机器学习实用指南》 第1章 机器学习概览
查看>>