抓取百度或者其他网站数据没问题,可是谷歌的总是报错:java.net.SocketException: Connection reset
抓取代码如下:
                  HttpURLConnection connection = null;
BufferedInputStream in = null;
BufferedReader read = null;

try {
URL url = new URL(strURL); 

String cookie = "";
int iii=0;
do {
iii++;
connection = (HttpURLConnection)url.openConnection();
System.out.println("11111111:"+strURL);
Thread.sleep(5000);
if(cookie.length() != 0) {
connection.setRequestProperty("Cookie", cookie);
}

connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)");
connection.setInstanceFollowRedirects(false);
System.out.println("9999999999"+connection.getHeaderField("Set-Cookie"));
int code = connection.getResponseCode();                   if(code == HttpURLConnection.HTTP_MOVED_TEMP) {
                      cookie += connection.getHeaderField("Set-Cookie") + ";";
                  }                   if((connection.getResponseCode() == HttpURLConnection.HTTP_OK) || iii==4)
                  break;

}
while(true); in = new BufferedInputStream(connection.getInputStream());
read = new BufferedReader(new InputStreamReader(in,"GB2312"));

解决方案 »

  1.   

    System.out.println("11111111:"+strURL);
    部分可以争取打印出来
    System.out.println("9999999999"+connection.getHeaderField("Set-Cookie"));
    部分打印结果为:9999999999null
      

  2.   

    google是做Web服务起家的,你想用程序获取人家服务的结果,google能让你这么轻易拿劳动成果吗?google map,google translate,google doc...几乎所有的google产品你都可以集成到你自己的web应用中,但是:要给钱!!!!记得以前集成google map的地图搜索的时候,社区版还是免费的,但是一天内只能搜索20次....
      

  3.   

    贴出你抓取的GOOGLE的完整地址
      

  4.   

    http://www.google.com.hk/search?num=50&hl=zh-CN&lr=&newwindow=1&safe=strict&tbs=qdr%3Ad&q=关键词+site%3Aclub.xcar.com&btnG=Google+%E6%90%9C%E7%B4%A2&aq=f&aqi=&aql=&oq=
    其中关键词,已经转码了
      

  5.   


    能把你的代码给我看看吗,或者看看我的有什么问题?我没设置代理服务器。我的代码抓别的网站没问题,就是google不行呢。
      

  6.   

    google 做了设定了吧.. 你给的URL 我在浏览器的地址栏上敲不开..说连接重置
      

  7.   

    理论上来说手动搜和代码搜能有什么本质的区别呢,手动搜最终也是通过程序提交的,就得考虑这两者的区别
    修改下面这句或者不要下面这句:
    connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)");
    把Mozilla/4.0 (compatible; MSIE 8.0)修改成其它串
      

  8.   

    楼上说的是访问时.. 拼接头文件? 对接TCP/IP 协议?我用我的火狐 看到google的是这个.. Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-cn; rv:1.9.2.12) Gecko/20101028 Firefox/3.6.12 (Palemoon/3.6.12)楼主把头文件都拼接下.再访问吧
      

  9.   

    正想学习一下这个,我喜欢用js+ajax去抓