日期:2014-05-16  浏览次数:20312 次

使用JSoup进行新浪微博抓取(不用新浪的API)

本文来自:fair-jm.iteye.com 转截请注明出处

额 就是想做个简单的实验的 内容不对的地方欢迎拍砖...

?

使用JSOUP就行 这里给出点思路

我只做了自己的首页和其他人的微博首页的抓取 其他的抓取没尝试(不好意思 比较懒...)?

?

首先是利用JSOUP进行登陆 获取页面 看了下微博的登陆表格 发现用了ajax的方式 所以代码获取cookie有点难

所以偷了个懒就用IE的开发者工具获取到了cookie 获取到的cookie要写成map的形式 然后用代码:

Response res=Jsoup.connect("http://weibo.com").cookies(map).method(Method.POST).execute();
String s=res.body();

?得到了下发现挺多的:



?可以自己写段脚本来打印map.put(xxx,xxx)

我这里用scala写了段 用java写一样的 无所谓:

s.split("; ").foreach(s => {val x=s.split("=");println(s"""map.put("${x(0)}","${x(1)}");""")});

?最后得到的body 嗯......是一大堆的script标签 最上面是微博的固定的顶上那一栏的内容(导航条的内容)

lz尝试了下 发现需要的是 <script>FM.view 中一个id为pl_content_homeFeed的 他就是首页的内容

然后lz进行了下简单的处理 没有用正则 因为....额...写不好:

        String s=res.body();
        //System.out.println(s);
        String[] ss=s.split("<script>FM.view");
        int i=0;
        //pl_content_homeFeed
//        for(String x:ss){
//        	System.out.println(i++ + "======================================");
//        	System.out.println(x.substring(0, x.length()>100?100:x.length()));
//        	System.out.println("===========================================");
//        }
        String content=ss[8].split("\"html\":\"")[1].replaceAll("\\\\n", "").replaceAll("\\\\t", "").replaceAll("\\\\", "");
        content=content.substring(0, content.length()<=13?content.length():content.length()-13);
        System.out.println(content);

?输出的content就是首页显示的微博内容?

不过这个输出的话unicode没有被转成中文字符 需要用native2ascii工具 去网上找到了一个:

http://soulshard.iteye.com/blog/346807

实测可以使用:

System.out.println(Native2AsciiUtils.ascii2Native(content));

?

注意了 以上的代码 lz是固定了主页的 所以在截取时直接用了index为8的

把post方法改成get方法 也可以获取到其他人的微博页?

然后给出一个打印出获取的所有html内容的做法(试了一些主页可行):

package jsoupTest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;

public class JsoupTest {
	public static void main(String[] args) throws IOException {
		Map<String, String> map = new HashMap<>();
		//map.put请根据自己的微博cookie得到

		Response res = Jsoup.connect("http://weibo.com/u/别人的主页id")
				.cookies(map).method(Method.GET).execute();
		String s = res.body();
		System.out.println(s);
		String[] ss = s.split("<script>FM.view");
		int i = 0;
		// pl_content_homeFeed
		// pl.content.homeFeed.index
		List<String> list = new ArrayList<>();
		for (String x : ss) {
//			System.out.println(i++ + "======================================");
//			System.out.println(x.substring(0,
//					x.length() > 200 ? 200 : x.length()));
//			System.out.println("===========================================");
			if (x.contains("\"html\":\"")) {
				String value = getHtml(x);
				list.add(value);
				System.out.println(value);
			}

		}
		// content=ss[8].split("\"html\":\"")[1].replaceAll("(\\\\t|\\\\n)",
		// "").replaceAll("\\\\\"", "\"").replaceAll("\\\\/", "/");
		// content=content.substring(0,
		// content.length()<=13?content.length():content.length()-13);
		// System.out.println(Native2AsciiUtils.ascii2Native(content));
	}

	public static String getHtml(String s) {
		String content = s.split("\"html\":\"")[1]
				.replaceAll("(\\\\t|\\\\n|\\\\r)", "").replaceAll("\\\\\"", "\"")
				.replaceAll("\\\\/", "/");
		content = content.substring(0,
				content.length() <= 13 ? content.length()
						: content.length() - 13);
		return Native2AsciiUtils.ascii2Native(content);
	}
}