java 如何大量访问某个网站

sinat_27567493 sinat_27567493 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我想用java做一个程序来大量摘取某网站的特定内容
下面的函数是用来建立链接并读取网站html源码的。

public static String getHtmlCode(String path) {
String html = "", temp = "";
try {
URL u = new URL(path);
URLConnection connection = u.openConnection();
HttpURLConnection htCon = (HttpURLConnection) connection;
htCon.addRequestProperty("baiduspider User-Agent", "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");
int code = htCon.getResponseCode();
if (code == HttpURLConnection.HTTP_OK) {
System.out.println("find the website");
BufferedReader in=new BufferedReader(new InputStreamReader(htCon.getInputStream()));
while ((temp = in.readLine()) != null)
html = html + " " + temp;
in.close();
htCon.disconnect();
}
else {
System.out.println("Can not access the website");
}
}
catch(MalformedURLException e ) {
System.out.println("Wrong URL");
}
catch(IOException e) {
System.out.println("Can not connect");
}
return html;
}

现在的问题是:如果这段代码连续执行次数达到20次左右,网站就不再返回代码了,而是返回空白,要过一段时间后才能再有内容。我怀疑是网站禁止了我的ip,可是用浏览器却可以访问网站。而且就算我降低频率到每3秒读取一次,还是会发生上述情况,有人知道是怎么回事吗?
...全文
评论 14
收藏 已收藏
分享
返回顶部
sinat_27567493
Java SE 62,621 成员 307,251 内容