Java模拟登陆新浪微博抓取数据【转载】
package com.shiyimm.crawler.weibo; import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException; import net.sf.json.JSONObject; import org.apache.commons.codec.binary.Base64;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair; import com.shiyimm.crawler.util.MyUrlUtil;
import com.shiyimm.crawler.util.UrlUtil; public class SinaWeibo {
private HttpClient client;
private String username; //登录帐号(明文)
private String password; //登录密码(明文)
private String su; //登录帐号(Base64加密)
private String sp; //登录密码(各种参数RSA加密后的密文)
private long servertime; //初始登录时,服务器返回的时间戳,用以密码加密以及登录用
private String nonce; //初始登录时,服务器返回的一串字符,用以密码加密以及登录用
private String rsakv; //初始登录时,服务器返回的一串字符,用以密码加密以及登录用
private String pubkey; //初始登录时,服务器返回的RSA公钥 private String errInfo; //登录失败时的错误信息
private String location; //登录成功后的跳转连接 private String url; public SinaWeibo(String username,String password){
client = new DefaultHttpClient();
this.username = username;
this.password = password;
} /**
* 初始登录信息<br>
* 返回false说明初始失败
* @return
*/
public boolean preLogin(){
boolean flag = false;
try {
su = new String(Base64.encodeBase64(URLEncoder.encode(username, "UTF-8").getBytes()));
String url = "http://login.sina.com.cn/sso/prelogin.php?entry=weibo&rsakt=mod&checkpin=1&" +
"client=ssologin.js(v1.4.5)&_="+getTimestamp();
url += "&su="+su;
String content;
content = HttpTools.getRequest(client, url);
//System.out.println(content);
System.out.println("content------------"+content);
JSONObject json = JSONObject.fromObject(content);
System.out.println(json);
servertime = json.getLong("servertime");
nonce = json.getString("nonce");
rsakv = json.getString("rsakv");
pubkey = json.getString("pubkey");
flag = encodePwd();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
return flag;
} /**
* 登录
* @return true:登录成功
*/
public boolean login(){
if(preLogin()){
String url = "http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.5)";
List<NameValuePair> parms = new ArrayList<NameValuePair>();
parms.add(new BasicNameValuePair("entry", "weibo"));
parms.add(new BasicNameValuePair("geteway", "1"));
parms.add(new BasicNameValuePair("from", ""));
parms.add(new BasicNameValuePair("savestate", "7"));
parms.add(new BasicNameValuePair("useticket", "1"));
parms.add(new BasicNameValuePair("pagerefer", "http://login.sina.com.cn/sso/logout.php?entry=miniblog&r=http%3A%2F%2Fweibo.com%2Flogout.php%3Fbackurl%3D%2F"));
parms.add(new BasicNameValuePair("vsnf", "1"));
parms.add(new BasicNameValuePair("su", su));
parms.add(new BasicNameValuePair("service", "miniblog"));
parms.add(new BasicNameValuePair("servertime", servertime+""));
parms.add(new BasicNameValuePair("nonce", nonce));
parms.add(new BasicNameValuePair("pwencode", "rsa2"));
parms.add(new BasicNameValuePair("rsakv", rsakv));
parms.add(new BasicNameValuePair("sp", sp));
parms.add(new BasicNameValuePair("encoding", "UTF-8"));
parms.add(new BasicNameValuePair("prelt", "182"));
parms.add(new BasicNameValuePair("url", "http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack"));
parms.add(new BasicNameValuePair("returntype", "META"));
try {
String content = HttpTools.postRequest(client, url, parms);
System.out.println("content----------"+content);
String regex = "location\\.replace\\(\"(.+?)\"\\);";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(content);
if(m.find()){
location = m.group(1);
if(location.contains("reason=")){
errInfo = location.substring(location.indexOf("reason=")+7);
errInfo = URLDecoder.decode(errInfo, "GBK");
}else{
String result = HttpTools.getRequest(client, location);
System.out.println("result--------------"+result);
return true;
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// url = "http://www.weibo.com/hm";
// System.out.println(MyUrlUtil.getResource(url));
}
return false;
} /**
* 密码进行RSA加密<br>
* 返回false说明加密失败
* @return
*/
private boolean encodePwd(){
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine se = sem.getEngineByName("javascript");
try {
FileReader fr = new FileReader("E:\\encoder.js");
se.eval(fr);
Invocable invocableEngine = (Invocable) se;
String callbackvalue = (String) invocableEngine.invokeFunction("encodePwd",pubkey,servertime,nonce,password);
sp = callbackvalue;
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("加密脚本encoder.sj未找到");
} catch (ScriptException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
errInfo = "密码加密失败!";
return false;
} public String getErrInfo() {
return errInfo;
} /**
* 获取时间戳
* @return
*/
private long getTimestamp(){
Date now = new Date();
return now.getTime();
} public static void main(String[] args) throws ClientProtocolException, IOException {
SinaWeibo weibo = new SinaWeibo("账号", "密码");
if(weibo.login()){
System.out.println("登陆成功!");
String url = "http://www.weibo.com/hm";
// String source = MyUrlUtil.getResource(url);
// System.out.println(source);
}else{
System.out.println("登录失败!");
}
}
}
<pre class="brush:java; toolbar: true; auto-links: false;">package com.shiyimm.crawler.weibo; import java.io.IOException;
import java.util.List; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.util.EntityUtils; public class HttpTools {
/**
* 正常GET方式HTTP请求
* @param client
* @param url
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String getRequest(HttpClient client,String url) throws ClientProtocolException, IOException{
HttpGet get = new HttpGet(url);
get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11");
//get.addHeader("Referer", "http://2013.weibo.com/");
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity,"GBK");
//System.out.println(content);
/*EntityUtils.consume(entity);*/
return content;
} /**
* 正常POST方式HTTP请求
* @param client
* @param url
* @param parms
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String postRequest(HttpClient client,String url,List<NameValuePair> parms) throws ClientProtocolException, IOException{
HttpPost post = new HttpPost(url);
post.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11");
post.addHeader("Content-Type", "application/x-www-form-urlencoded");
//post.addHeader("Referer", "http://2013.weibo.com/");
UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(parms, "UTF-8");
post.setEntity(postEntity);
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity,"GBK");
/*EntityUtils.consume(entity);*/
return content;
}
}
Java模拟登陆新浪微博抓取数据【转载】的更多相关文章
- Scrapy模拟登陆豆瓣抓取数据
scrapy startproject douban 其中douban是我们的项目名称 2创建爬虫文件 进入到douban 然后创建爬虫文件 scrapy genspider dou douban. ...
- 测试开发Python培训:抓取新浪微博抓取数据-技术篇
测试开发Python培训:抓取新浪微博抓取数据-技术篇 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.在poptest的se ...
- 【转】详解抓取网站,模拟登陆,抓取动态网页的原理和实现(Python,C#等)
转自:http://www.crifan.com/files/doc/docbook/web_scrape_emulate_login/release/html/web_scrape_emulate_ ...
- PHP Curl模拟登录并抓取数据
使用PHP的Curl扩展库可以模拟实现登录,并抓取一些需要用户账号登录以后才能查看的数据.具体实现的流程如下(个人总结): 1. 首先需要对相应的登录页面的html源代码进行分析,获得一些必要的信息: ...
- PHP获取cookie、Token、模拟登录、抓取数据、解析生成json
本文介绍使用PHP获取cookie,获取Token.以及模拟登录.然后抓取数据.最后解析生成json的的过程. 0. 设置Cookie路径 set_time_limit(0); //使用的cookie ...
- Java模拟登录系统抓取内容【转载】
没有看考勤的习惯,导致我的一天班白上了,都是钱啊,系统也不发个邮件通知下.... 为了避免以后还有类似状况特别写了个java模拟登录抓取考勤内容的方法(部分代码来自网络),希望有人修改后也可以 ...
- Java模拟新浪微博登陆抓取数据
前言: 兄弟们来了来了,最近有人在问如何模拟新浪微博登陆抓取数据,我听后默默地抽了一口老烟,暗暗的对自己说,老汉是时候该你出场了,所以今天有时间就整理整理,浅谈一二. 首先: 要想登陆新浪微博需要 ...
- php中CURL技术模拟登陆抓取数据实战,抓取某校教务处学生成绩。
这两天有基友要php中curl抓取教务处成绩的源码,用于微信公众平台的开发.下面笔者只好忍痛割爱了.php中CURL技术模拟登陆抓取数据实战,抓取沈阳工学院教务处学生成绩. 首先,教务处登录需要验证码 ...
- java使用htmlunit工具抓取js中加载的数据
htmlunit 是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容.项目可以模拟浏览器运行,被誉为java浏览器的开源实现.这个没有界面的浏览器,运行速度 ...
随机推荐
- Oracle中job的使用详解
我们在项目开发中,常常会有一些复杂的业务逻辑.使用oracle的存储过程,可以大大减少java程序代码的编写工作量,而且存储过程执行在数据库上,这样可以利用oracle的良好性能支持,极大地提高程序执 ...
- Oracle alter index rebuild 与 ORA-08104 说明
在ITPUB 论坛上看到的一个帖子,很不错.根据论坛的帖子重做整理了一下. 原文链接如下: alter index rebuild online引发的血案 http://www.itpub.net/t ...
- Android 嵌套GridView,ListView只显示一行的解决办法
重写ListView.GridView即可: public class MyListView extends ListView { public MyListView(Context context) ...
- 用Apache Kafka构建流数据平台的建议
在<流数据平台构建实战指南>第一部分中,Confluent联合创始人Jay Kreps介绍了如何构建一个公司范围的实时流数据中心.InfoQ前期对此进行过报道.本文是根据第二部分整理而成. ...
- Android 打勾显示输入的密码
main.xml: <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:and ...
- Slalom
题意: 有n个宽度为w的门,给出门的左端点的水平位置x和高度y,和恒定的垂直速度,现有s个速度,求能通过这n个门的最大速度. 分析: 二分速度判断 #include <map> #incl ...
- XCode修改工程名注意
以下文字转载过来,在使用的过程中遇到几个问题 1.需要在 Build phases 里面,检查下 Link Binary With Libraries 以及Compline Sources 2.Bul ...
- ADO.NET 中的数据并发
当多个用户试图同时修改数据时,需要建立控制机制来防止一个用户的修改对同时操作的其他用户所作的修改产生不利的影响.处理这种情况的系统叫做“并发控制”.并发控制的类型通常,管理数据库中的并发有三种常见的方 ...
- redo文件一
redo log files and redo log buffer redo log files的作用的是确保数据库崩溃之后能正确的恢复数据库,恢复数据库到一,致性的状态 redo log file ...
- QT-【转】基础(略)
第0篇 开始学习Qt 与Qt Creator 第1篇 基础(一)Qt开发环境的搭建和hello world 第2篇 基础(二)编写Qt多窗口程序 第3篇 基础(三)Qt登录对话框 第4篇 基础(四)添 ...