花花_新世界
从【程序猿】到【程序员】的进化历程
posts - 60,comments - 10,views - 12万

废话不多说直接上代码:(亲测可用)

复制代码
  1 package plugins.timer.surfacewater;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.File;
  5 import java.io.IOException;
  6 import java.io.InputStream;
  7 import java.io.InputStreamReader;
  8 import java.util.ArrayList;
  9 import java.util.List;
 10 
 11 import org.xml.sax.SAXException;
 12 import plugins.timer.station.SurfaceWaterModel;
 13 
 14 public class JSUtil {
 15 
 16     // 如果要更换运行环境,请注意exePath最后的phantom.exe需要更改。因为这个只能在window版本上运行。前面的路径名
 17             // 也需要和exePath里面的保持一致。否则无法调用
 18             private static String projectPath = System.getProperty("catalina.home");
 19             private static String jsPath = projectPath +  File.separator+ "webapps"+File.separator+"syads"+File.separator+"huicong.js";
 20             private static String exePath = projectPath +  File.separator+"webapps"+File.separator+"syads"+File.separator+ "phantomjs"
 21                     + File.separator + "bin" + File.separator + "phantomjs.exe";
 22             /*private static String projectPath = System.getProperty("user.dir");
 23             private static String jsPath = projectPath + File.separator+"WebRoot"+File.separator+"huicong.js";
 24             private static String exePath = projectPath +File.separator+"WebRoot"+File.separator+ "phantomjs"
 25                     + File.separator + "bin" + File.separator + "phantomjs.exe";*/
 26 
 27     
 28      32     public String JSUtil() throws IOException, SAXException {
 33 
 34         // 测试调用,传入url即可,第二个参数是要读取的字节数
 35         String str = getParseredHtml2("http://123.127.175.45:8082/",30000);
 36         
 37         System.out.println("str:"+str);
 38         return str;
 39         
 40         
 41     }
 42 
 43     // 调用phantomjs程序,并传入js文件,并通过流拿回需要的数据。
 44     public static String getParseredHtml2(String url,int num) throws IOException {
 45         Runtime rt = Runtime.getRuntime();
 46         Process p = rt.exec(exePath + " " + jsPath + " " + url);
 47         InputStream is = p.getInputStream();
 48         BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
 49         StringBuffer sbf = new StringBuffer();
 50         String tmp = "";
 51         while ((tmp = br.readLine()) != null) {
 52             sbf.append(tmp);
 53         }
 54         System.out.println("sbf:"+sbf);
 55         //得到整个网页的字符串
 56         String tableStr = sbf.toString();
 57         return tableStr;
115     }
116     /**
117      * 返回给定地址中的数据
118      * @param url
119      * @return
120      */
121     public static String getData(String url){
122         int start = url.indexOf("%\">")+3;
123         int end = url.indexOf("<",start);
124         return url.substring(start,end);
125     }
126 
127 }
复制代码

   注意:

  • 程序中需要用到两个工具,分别是huicong.js和phantomjs.exe。这两个工具我已经打包放到了百度云盘里:

    微信关注公众号“姓氏头像宝典”,回复爬虫即可

  • syads是我的项目名称
  • http://123.127.175.45:8082/是测试的网站
  • 30000是指爬取的字节数
posted on   花花_新世界  阅读(512)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

了解更多