`
elan1986
  • 浏览: 164525 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JAVA抓取百度指数数据

阅读更多
在论坛看帖子看到一则抓取百度指数的需求,自己最近刚好看到httpclient和httpparser

思路:
1、查看百度指数页面,找出页面的编码方式。
2、浏览器提交一些测试数据,并观察浏览器地址栏的变化。
3、httpclient测试是否可以读取该页面的数据内容
4、拼凑百度地址栏的数据信息

PS:比较简单 就没有多余的注释信息
代码如下:


package com.lch.hibaidu;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URLEncoder;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class GetIndex {

	public static void main(String[] args) throws Exception {
		
		String QueryString = "3Q";
		String URLQueryString = URLEncoder.encode(QueryString);

		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpHost targetHost = new HttpHost("index.baidu.com");

		HttpGet httpGet = new HttpGet("/main/word.php?word="+URLQueryString);
		System.out.println("目标: " + targetHost);
		System.out.println("请求: " + httpGet.getRequestLine());

		HttpResponse response = httpClient.execute(targetHost, httpGet);
		HttpEntity entity = response.getEntity();
		System.out.println("---------------------------------");
		System.out.println(response.getStatusLine());

		if (entity != null) {
			System.out.println("Response content length : "
					+ entity.getContentLength());
		}

		BufferedReader buReader = new BufferedReader(new InputStreamReader(
				entity.getContent(), "gb2312"));
		String line = null;
		while ((line = buReader.readLine()) != null) {
			System.out.println(line);
		}
		if (entity != null) {
			entity.consumeContent();
		}
	}
}


得到数据结果信息后,可以通过httpparser进行分析,这个就不多说了!
1
1
分享到:
评论
4 楼 jrius 2012-12-06  
这种方式 应该是抓不到的,百度指数使用了amf格式
3 楼 lenxeon 2011-04-14  
[url]http://www.zendlab.com/soso/baiduIndex/
这个网站可以实时查询。[/url]
2 楼 elan1986 2010-11-11  
kfliyangfan 写道
用户关注度flash里面的数据能取出来吗?

哈哈,刚好今天夜晚在研究百度文库、豆丁文档系统的东西,刚写了几个小例子,用JAVA生成SWF,我想应该可以读取里面的数据吧,但是我还没有尝试。只是一种猜测!
1 楼 kfliyangfan 2010-11-11  
用户关注度flash里面的数据能取出来吗?

相关推荐

Global site tag (gtag.js) - Google Analytics