java 如何大量访问某个网站
我想用java做一个程序来大量摘取某网站的特定内容
下面的函数是用来建立链接并读取网站html源码的。
现在的问题是:如果这段代码连续执行次数达到20次左右,网站就不再返回代码了,而是返回空白,要过一段时间后才能再有内容。我怀疑是网站禁止了我的ip,可是用浏览器却可以访问网站。而且就算我降低频率到每3秒读取一次,还是会发生上述情况,有人知道是怎么回事吗?
下面的函数是用来建立链接并读取网站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秒读取一次,还是会发生上述情况,有人知道是怎么回事吗?
...全文
相关推荐
JAVA开发者最常去的20个英文网站
JAVA开发者最常去的20个英文网站.JAVA开发者最常去的20个英文网站.JAVA开发者最常去的20个英文网站.JAVA开发者最常去的20个英文网站.JAVA开发者最常去的20个英文网站.
[myOnlinesWeb]网站在线用户信息,网站访问量
[myOnlinesWeb]网站在线用户信息,网站访问量...
Java开发中几个可以常去看的外国网站
Java开发中几个可以常去看的外国网站
java有关的网站
NULL 博文链接:https://jiangzhenwei6.iteye.com/blog/975536
宣传网站《品红》大量jsp+sql
宣传网站《品红》大量jsp+sql,src,webroot30多个jsp页面


Java SE
