java爬取异步数据_Java 关于抓取异步的网页数据

本文介绍如何使用Java解决抓取采用AJAX异步加载的网页数据的问题。通过设置HTTP连接参数,模拟浏览器行为,以获取完整的HTML内容。然而,对于异步加载的数据,通常需要解析JavaScript或者利用更复杂的爬虫库如Selenium来获取。
摘要由CSDN通过智能技术生成

publicstaticStringgetHtmlCode(Stringurl,StringcharCode)throwsIOException{StringhtmlCode="";InputStreamin=null;HttpURLConnectionconnection=null;try{URLurlCon=newURL(url);c...

public static String getHtmlCode(String url,String charCode) throws IOException {

String htmlCode = "";

InputStream in = null;

HttpURLConnection connection=null;

try {

URL urlCon = new URL(url);

connection = (HttpURLConnection) urlCon.openConnection();

connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.25 Safari/534.24");

connection.setConnectTimeout(5000);

connection.setReadTimeout(5000);

connection.setFollowRedirects(true);

connection.setInstanceFollowRedirects(false);

connection.setDefaultUseCaches(false);

in = urlCon.openStream();

int index = 0;

byte[] bytes = new byte[1024*100];

int count = in.read(bytes, index, 1024 * 100);

while (count != -1) {

index += count;

count = in.read(bytes, index, 1);

}

htmlCode = new String(bytes, charCode);

} catch (Exception e) {

e.printStackTrace();

}

finally{

if(in!=null)

in.close();

if(connection!=null)

connection.disconnect();

}

return htmlCode;

}

我要抓取一个页面的数据,但是因为访问的页面采用AJAX。使我读取的数据不完全。请问下该怎么写才能读取到全部的数据

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值