微博模拟登录获取cookis,配置采集深度,采集一条微博转发关系页面,同时解析页面,生成一条微博的传播图,数据集可做微博影响力分析和传播分析

gitthub:https://github.com/czeze/WeiboCrwlZEZE

package main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.MalformedURLException;
import java.util.List;
import java.util.Random; import org.apache.http.client.CookieStore;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.Cookie;
import com.sun.jna.Native.ffi_callback; import Util.FileWriteUtil;
import Util.Id2MidUtil; /**
*
* @ClassName: SourceWeiboCrawler
* @Description: 一条微博信息采集,输入微博URL
* @author Zeze
* @date 2016年4月10日 上午10:39:50
*
*/
public class WeiboCrawler2 { private static String Url = "http://weibo.com/5892492312/DpsRXpOyG?from=page_1005055892492312_profile&wvr=6&mod=weibotime&type=comment";
private static int CrawlDeep = 18;// 采集深度
private static int SleepTime = 1000;// 采集间隔时间
private static int NumCookies=7;//cookies数目 private static Logger logger = Logger.getLogger(WeiboCrawler2.class);
private static String cookiePath = "F:/WeiBo/cookie/cookie.file";// cookie目录
private static String outputpath = "F:/WeiBo/Data/";// 输出目录
private static String destfile = "F:/WeiBo/Data/";// 采集保存目录
private static int cnt = 0; public static void main(String[] args) { String mid = GetMid(Url);// D8hxnrQdM
String uid = GetUid(Url);// outputpath = outputpath + mid + "/";// 输出目录
destfile = destfile + mid + "/msg" + mid + ".csv";// 采集保存目录 File file2 = new File(outputpath);
if (!file2.exists())
file2.mkdirs();
String wString = "tzmid,zid,zname,zmid,mid,ztext,zurl,zsource,zzan,ztime,deep";
StringBuffer sBuilder = new StringBuffer();
sBuilder.append(wString + "\r\n");
FileWriteUtil.WriteDocument(destfile, sBuilder.toString()); // 获得页数
int PageNum = GetPageNum(mid, uid, 0, CrawlDeep); for (int i = 1; i <= PageNum; i++) {

(mid, uid, Integer.toString(i), 0, CrawlDeep);
try {// 采集间隔
Thread.sleep(SleepTime);
} catch (InterruptedException e) {
logger.error(e);
return;
}
}
} webClient.closeAllWindows();
return Num;
} //转发内容
if (info.select("span[class=cmt]").text().equals("")) {// 不是转发的内容
text = info.select("span[class=ctt]").text();// 正文 }
// 时间
Elements time = info.select("span[class=ct]");
//转发
Elements rt = doc.select("div").select("span[id=rt]");
//评论
Elements ct = doc.select("div").select("span:contains(评论)");
//赞
Elements zan = doc.select("div").select("span:contains(赞)"); text = text.trim();
name = BoZhu.get(0).text().trim();
timeStr = time.text().trim();
ZhuanFaNum = rt.text().trim().substring(2).replace("[", "").replace("]", "");
PinlunNum = ct.text().trim().substring(3).replace("[", "").replace("]", "");
zanNum = zan.get(0).text().trim().substring(2).replace("[", "").replace("]", ""); if (!rt.text().contains("["))//判断是否有转发
ZhuanFaNum = "0";
if (!ct.text().contains("["))
PinlunNum = "0"; System.out.println("英文消息ID: " + mid);
System.out.println("数字消息ID: " + Nummid);
System.out.println("用户ID: " + uid);
System.out.println("博主: " + name);
System.out.println("正文内容: " + text); String zname = null;// 转发的用户名
String zid = null;// 转发的用户ID
String zzan = null;// 点赞数
String zmid = null;// 转发的消息id
String ztime = null;// 转发时间
String zsource = null;// 来源
String ztext = null;// 转发的内容
String zurl = null; if (!rt.text().contains("[")) {
System.out.println("没有转发");
try {// 采集间隔1s
Thread.sleep(SleepTime);
} catch (InterruptedException e) {
logger.error(e);
}
return;
} if (doc.select("[id=pagelist]").text().contains("页")) {//转发页数
String pnum = doc.select("[id=pagelist]").get(0).text();
pnum = pnum.substring(pnum.indexOf("/") + 1).replace("页", "");
System.out.println("转发页数:" + pnum);
} for (Element result : RTList) {// 解析列表
// 点赞数
zzan = result.select("span[class=cc]").text();
if (zzan.equals("")) {// 过滤没有点赞标签
continue;
} zzan = zzan.trim().substring(1).replace("[", "").replace("]", ""); if (result.select("a").size() > 0) {
zname = result.select("a").get(0).text();// 转发的用户名
zid = result.select("a").get(0).toString();// 转发的用户id
if (zid.indexOf("u") == 10) {// 正常的用户id
zid = zid.substring(zid.indexOf("\">") - 10, zid.indexOf("\">"));
} else {
zid = zid.substring(zid.indexOf("/") + 1, zid.indexOf("\">"));
}
}
if (result.text().contains("查看更多热门"))
continue; // 转发时间和来源
String tmp = result.select("span[class=ct]").text();
ztime = tmp.substring(0, tmp.indexOf("来自"));
zsource = tmp.substring(tmp.indexOf("来自") + 2);
text.indexOf("赞"));
} zurl = "http://weibo.cn/repost/" + zmid + "?uid=" + zid;
String tzmid = new Id2MidUtil().Uid2Mid(zmid); // 消息ID,用户ID,用户名,屏幕名,转发消息ID,消息内容,消息URL,来源,赞数,发布时间,层数
// tzmid,zid,zname,zmid,mid,ztext,zurl,zsource,zzan,ztime,deep
cnt++;
String wString = tzmid + "," + zid + "," + zname + "," + zmid + "," + Nummid + "," + ztext + "," + zurl + ","
+ zsource + "," + zzan + "," + ztime + "," + deep;
System.out.println(cnt + ":" + wString); StringBuffer sBuilder = new StringBuffer();
sBuilder.append(wString + "\n");
FileWriteUtil.WriteDocument(destfile, sBuilder.toString()); }
} }
System.out.println("采集到的转发数目:" + cnt);
return;
} // 解析第二页开始
public static void parserPage(HtmlPage page, String mid, String uid, int deep, int crawldeep) {
String html = page.getWebResponse().getContentAsString();
Document doc = Jsoup.parse(html); String zname = null;// 转发的用户名
String zid = null;// 转发的用户ID
String zzan = null;// 点赞数
String zmid = null;// 转发的消息id
String ztime = null;// 转发时间
String zsource = null;// 来源
String ztext = null;// 转发的内容
String zurl = null; mid = new Id2MidUtil().Uid2Mid(mid);// 消息ID
Elements RTList = doc.select("div[class =c]");
for (Element result : RTList) {
// 点赞数
zzan = result.select("span[class=cc]").text();
if (zzan.equals("")) {// 过滤没有点赞标签
continue;
} zzan = zzan.trim().substring(1).replace("[", "").replace("]", ""); if (result.select("a").size() > 0) {
zname = result.select("a").get(0).text();// 转发的用户名
zcontinue; // 转发时间和来源
String tmp = result.select("span[class=ct]").text();
ztime = tmp.substring(0, tmp.indexOf("来自"));
zsource = tmp.substring(tmp.indexOf("来自") + 2); // 转发的消息id
zmid = result.select("span[class=cc]").toString();
zmid = zmid.substring(zmid.indexOf("attitude") + 9, zmid.indexOf("attitude") + 18); // 转发的内容
ztext = result.text();
if (ztext.contains("//@")) {
ztext = ztext.substring(ztext.indexOf(":") + 1, ztext.indexOf("//@"));
} else if(ztext.contains(":")&&ztext.contains("赞")){
ztext = ztext.substring(ztext.indexOf(":") + 1, ztext.indexOf("赞"));
}
deep;
System.out.println(cnt + ":" + wString); StringBuffer sBuilder = new StringBuffer();
sBuilder.append(wString + "\n");
FileWriteUtil.WriteDocument(destfile, sBuilder.toString()); if (deep < crawldeep) {// 采集深度
int PageNum = GetPageNum(zmid, zid, deep + 1, crawldeep);
for (int i = 1; i <= PageNum; i++) {
System.out.println("当前采集深度"+deep);
(zmid, zid, Integer.toString(i), deep + 1, crawldeep);
try {// 采集间隔1s
Thread.sleep(SleepTime);
} catch (InterruptedException e) {
logger.error(e);
return;
}
} }
}
System.out.println("采集到的转发数目:" + cnt);
return;
} // 写入文件
public static void SavePage(HtmlPage page, String path) { File file2 = null;
file2 = new File(path); if (file2.exists())
logger.warn("outfile exit!");
else {
FileOutputStream outputStream;
try {
outputStream = new FileOutputStream(file2);
outputStream.write(page.getWebResponse().getContentAsString().getBytes());
outputStream.close();
} catch (FileNotFoundException e) {
logger.error(e);
} catch (IOException e) {
logger.error(e);
}
}
} // 随机获取cookies
public static CookieStore GetCookieStore() {
CookieStore cookieStore = null;
File file = new File(cookiePathAppendRandom());
if (file.exists()) {
FileInputStream fin = null; ObjectInputStream in;
try {
in = new ObjectInputStream(fin);
cookieStore = (CookieStore) in.readObject();
in.close();
} catch (IOException e) {
logger.error(e);
} catch (ClassNotFoundException e) {
logger.error(e);
}
} else {
logger.warn("CookiePath doesn`t exit !!!");
}
return cookieStore;
} private static String cookiePathAppendRandom() {
Random random = new Random();
return cookiePath + random.nextInt(NumCookies);
} // http://weibo.com/1713926427/D8hxnrQdM?type=repost#_rnd1460261627854
public static String GetUid(String url) {
int index = url.indexOf("weibo.com") + 10; } public static String GetMid(String url) {
int index = url.indexOf("weibo.com") + 21;
return url.substring(index, index + 9);
} }

微博转发关系采集,可拓展关键字采集,评论采集(Java版)的更多相关文章

  1. HipHop算法:利用微博互动关系挖掘社交圈

    /* 版权声明:可以任意转载,转载时请务必标明文章原始出处和作者信息 .*/                  CopyMiddle: 张俊林 TimeStamp:2012年3 月 在微博环境下,如何 ...

  2. PAT Basic 1069. 微博转发抽奖(20)

    小明PAT考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔N个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行给出三个正整数M(<= 1000).N ...

  3. PAT 乙级 1069 微博转发抽奖(20) C++版

    1069. 微博转发抽奖(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 小明PAT考了满分,高兴之余决定 ...

  4. PAT 1069 微博转发抽奖(20)(代码+思路+测试点4)

    1069 微博转发抽奖(20 分) 小明 PAT 考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔 N 个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行 ...

  5. PAT 1069 微博转发抽奖

    https://pintia.cn/problem-sets/994805260223102976/problems/994805265159798784 小明 PAT 考了满分,高兴之余决定发起微博 ...

  6. PAT——1069. 微博转发抽奖

    小明PAT考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔N个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行给出三个正整数M(<= 1000).N ...

  7. PAT 1069. 微博转发抽奖(20)

    小明PAT考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔N个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行给出三个正整数M(<= 1000).N ...

  8. PAT(B) 1069 微博转发抽奖(Java)

    题目链接:1069 微博转发抽奖 (20 point(s)) 题目描述 小明 PAT 考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔 N 个人就发出一个红包.请你编写程序帮助他确 ...

  9. PAT Basic 1069 微博转发抽奖 (20 分)

    小明 PAT 考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔 N 个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行给出三个正整数 M(≤ 1000). ...

随机推荐

  1. Java字符串方法

    1.字符串和字符数组的转换 .toCharArray() String str1 = "hello"; // //System.out.println(str1); char c[ ...

  2. Windows Platform Predefined Macros

    https://msdn.microsoft.com/en-us/library/b0084kay.aspx

  3. thinkphp删除

    $result = M('content')->where('id>0')->delete $result =M('content')->where(array('id'=&g ...

  4. Django笔记-登陆注册-1

    1.项目结构(eclipse+PyDev工具)(粘上来后代码缩进格式没有了,就先不整了) 2.关键代码: test_log03.settings.py INSTALLED_APPS = ( 'djan ...

  5. ASP.NET WebAPI 08 Message,HttpConfiguration,DependencyResolver

    ASP.NET WebAPI 08 Message,HttpConfiguration,DependencyResolver   Message WebAPI作为通信架构必定包含包含请求与响应两个方法 ...

  6. JavaBean出现的目的

    一.JavaBean出现的目的 由于只用JSP开发会发现使用了很多的Scriptlet,会使得jsp文件非常混乱.如果使用了JavaBean,则可以大大减少JSP中代码量. JSP的理想状态是只负责显 ...

  7. AWK命令的用法

    1.awk命令简介: awk是一种可以处理数据.产生格式化报表的语言,功能十分强大. awk的工作方式是读取数据,将每一行数据视为一条记录(record)每笔记录以字段分隔符分成若干字段,然后输出各个 ...

  8. Kafka【第一篇】Kafka集群搭建

    Kafka初识 1.Kafka使用背景 在我们大量使用分布式数据库.分布式计算集群的时候,是否会遇到这样的一些问题: 我们想分析下用户行为(pageviews),以便我们设计出更好的广告位 我想对用户 ...

  9. jquery 匿名函数的区别

    下面两个定义函数的方法是等价的 var test = function(val){alert(val);} function test(val){alert(val);} 都是定义了一个test()方 ...

  10. Yii2.0 实现的短信发送

    原文地址:http://www.phpxs.com/post/4245/ yii2-smserGithub项目主页 https://github.com/daixianceng/yii2-smser ...