Java实现百度贴吧自动签到器



出处http://blog.csdn.net/zhongkejingwang/article/details/39995909

   前段时间经常逛贴吧,每次都要手动签到,有时候也会忘记签到,很麻烦。于是就想用Java写个自动签到的工具并部署到实验室的服务器上,这样就可以常年自动签到了。于是就有了这篇文章,权当玩玩。

  看一下程序的执行情况:

   将此程序放到服务器后台运行就可以了,不用再担心断签~   



   要签到首先要登录然后再获取贴吧页面里的签到链接。由于Java自带的http的API使用起来很不方便,这里使用的是Apache的httpclient(只用到了三个jar包,后面会提供下载),用post方法提交表单数据即可,关于表单的item参数可以用chrome浏览器抓包分析一下(按F12)。登录地址选择百度的手机网页端:http://wappass.baidu.com/passport/login这个地址,这样的话服务器端识别为手机签到,可以加分。在代码中将用户名和密码提交到这个地址即可,在去年的时候抓包分析过这个页面提交的数据还是明文的....现在已经升级了,网页是经过加密后提交的密码,不过明文提交还是可以登录。


   登录完了需要获取贴吧首页中用户关注的所有贴吧,在这个地址http://tieba.baidu.com/mo,获取网页特定内容需要用到html解析工具,这里使用的是Jsoup,可以在这里看使用教程http://www.open-open.com/jsoup/


  获取到关注贴吧的首页然后把该贴吧的html页面拉下来一份,检索看看有没有包含“sign”字符串,没有表示已经签到了;有则获取对应的签到地址,访问改地址即可签到成功。


  关于添加依赖包:在eclipse中右键工程—>属性—>Java 构建路径—>库(L)里添加外部JAR,就可以了。


  关于怎么打包成可执行的jar包:这里有依赖的jar包,建议将依赖的jar包一块打包进去,可以使用eclipse打包,具体步骤:

右键工程—>导出—>Java—>可运行的jar包,点击下一步,在接下来的选项中选择Package required libraries into generated JAR:

                       

这样就可以很方便的把jar包上传到服务器执行了。否则手动打包需要写MANIFEST.MF清单文件,指定Class-Path,指向所有依赖包,否则运行时找不到依赖包,比较麻烦。所以还是建议用eclipse打包吧。。。


  过程讲完了可以讲代码了。写一个SignUpTool接口:

[java]   view plain copy
  1. package com.jingchen.util;  
  2.   
  3. public interface SignUpTool {  
  4.     /** 
  5.      * 用户登录 
  6.      *  
  7.      * @param username 
  8.      *            用户名 
  9.      * @param passwd 
  10.      *            密码 
  11.      * @return 登录成功返回true,失败则返回false 
  12.      */  
  13.     boolean login(String username, String passwd);  
  14.   
  15.     /** 
  16.      * 签到 
  17.      *  
  18.      * @return 签到成功返回true,失败则返回falses 
  19.      */  
  20.     boolean signUp();  
  21. }  
  接口包含了登录和签到方法,此接口不同的实现可以登录不同的网站。

写一个HttpUtil,封装post和get操作:

[java]   view plain copy
  1. package com.jingchen.util;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.http.HttpEntity;  
  6. import org.apache.http.HttpResponse;  
  7. import org.apache.http.client.ClientProtocolException;  
  8. import org.apache.http.client.CookieStore;  
  9. import org.apache.http.client.HttpClient;  
  10. import org.apache.http.client.methods.HttpGet;  
  11. import org.apache.http.client.methods.HttpPost;  
  12. import org.apache.http.client.protocol.ClientContext;  
  13. import org.apache.http.impl.client.BasicCookieStore;  
  14. import org.apache.http.impl.client.DefaultHttpClient;  
  15. import org.apache.http.protocol.BasicHttpContext;  
  16. import org.apache.http.protocol.HttpContext;  
  17. import org.apache.http.util.EntityUtils;  
  18.   
  19. public class HttpUtil {  
  20.     private HttpClient mHttpClient;  
  21.     private CookieStore mCookieStore;  
  22.     private HttpContext mContext;  
  23.     private HttpPost post;  
  24.     private HttpGet get;  
  25.   
  26.     public HttpUtil() {  
  27.     mHttpClient = new DefaultHttpClient();  
  28.     mCookieStore = new BasicCookieStore();  
  29.     mContext = new BasicHttpContext();  
  30.     }  
  31.   
  32.     public HttpResponse post(String url, HttpEntity he)  
  33.         throws ClientProtocolException, IOException {  
  34.     post = new HttpPost(url);  
  35.     post.setEntity(he);  
  36.     mContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore);  
  37.     HttpResponse hr = mHttpClient.execute(post, mContext);  
  38.     return hr;  
  39.     }  
  40.   
  41.     public String get(String url) throws ClientProtocolException, IOException {  
  42.     String result = null;  
  43.     get = new HttpGet(url);  
  44.     HttpResponse hr = mHttpClient.execute(get, mContext);  
  45.     result = EntityUtils.toString(hr.getEntity());  
  46.     return result;  
  47.     }  
  48. }  


实现SignUpTool接口的BaiduSignUp:

[java]   view plain copy
  1. package com.jingchen.util;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.apache.http.HttpEntity;  
  7. import org.apache.http.HttpResponse;  
  8. import org.apache.http.NameValuePair;  
  9. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  10. import org.apache.http.message.BasicNameValuePair;  
  11. import org.apache.http.util.EntityUtils;  
  12. import org.jsoup.Jsoup;  
  13. import org.jsoup.nodes.Document;  
  14. import org.jsoup.nodes.Element;  
  15. import org.jsoup.select.Elements;  
  16.   
  17. /** 
  18.  * 用于登录百度贴吧账号并签到的类 
  19.  *  
  20.  * @author jingchen 
  21.  *  
  22.  */  
  23. public class BaiduSignUp implements SignUpTool {  
  24.     // 登录链接  
  25.     private String mLoginUrl = null;  
  26.     // 登录后跳转的页面  
  27.     private String mIndexUrl = null;  
  28.     // 获取网页中的相对路径拼接上这个头部构成完整请求路径  
  29.     private String mUrlHead = null;  
  30.     // 是否需要验证码  
  31.     private boolean isAuth = false;  
  32.   
  33.     private HttpUtil httpUtil;  
  34.   
  35.     // 关注的贴吧  
  36.     private List<String> mLikeBars;  
  37.     // 关注的贴吧首页  
  38.     private List<String> mLikeBarsUrls;  
  39.   
  40.     public BaiduSignUp() {  
  41.     mLikeBars = new ArrayList<String>();  
  42.     mLikeBarsUrls = new ArrayList<String>();  
  43.     httpUtil = new HttpUtil();  
  44.     mLoginUrl = "http://wappass.baidu.com/passport/login";  
  45.     mIndexUrl = "http://tieba.baidu.com/mo";  
  46.     mUrlHead = "http://tieba.baidu.com";  
  47.     }  
  48.   
  49.     public boolean login(String username, String passwd) {  
  50.     isAuth = false;  
  51.     mLikeBars.clear();  
  52.     mLikeBarsUrls.clear();  
  53.     print("login...");  
  54.     List<NameValuePair> params = new ArrayList<NameValuePair>();  
  55.     params.add(new BasicNameValuePair("username", username));  
  56.     params.add(new BasicNameValuePair("password", passwd));  
  57.     HttpEntity he;  
  58.     try {  
  59.         he = new UrlEncodedFormEntity(params, "UTF-8");  
  60.         HttpResponse hr = httpUtil.post(mLoginUrl, he);  
  61.         String firstresult = EntityUtils.toString(hr.getEntity());  
  62.         if (firstresult.contains("verifycode")) {  
  63.         // 在异地登录或者登录频繁会出现验证码  
  64.         isAuth = true;  
  65.         print("需要验证码");  
  66.         return false;  
  67.         } else if (firstresult.contains("error_area")) {  
  68.         print("密码错误");  
  69.         return false;  
  70.         }  
  71.     } catch (Exception e) {  
  72.         e.printStackTrace();  
  73.         return false;  
  74.     }  
  75.     print("登录成功");  
  76.     return true;  
  77.     }  
  78.   
  79.     public boolean signUp() {  
  80.     if (isAuth) {  
  81.         print("需要验证码");  
  82.         return false;  
  83.     }  
  84.     print("signUp...");  
  85.     if (mLikeBars.size() != 0) {  
  86.         mLikeBars.clear();  
  87.         mLikeBarsUrls.clear();  
  88.     }  
  89.     if (!getLikeBars())  
  90.         return false;  
  91.     try {  
  92.         for (int i = 0; i < mLikeBars.size(); i++) {  
  93.         String barview = getWebContent(mLikeBarsUrls.get(i));  
  94.         if (!barview.contains("sign"))  
  95.             print(mLikeBars.get(i) + "已签到");  
  96.         else {  
  97.             Elements signurl = Jsoup.parse(barview)  
  98.                 .getElementsByAttributeValueMatching("href",  
  99.                     ".*sign.*");  
  100.             getWebContent(mUrlHead + signurl.attr("href"));  
  101.             print(mLikeBars.get(i) + "签到成功");  
  102.         }  
  103.         }  
  104.     } catch (Exception e) {  
  105.         e.printStackTrace();  
  106.         print("fail in signUp!");  
  107.         return false;  
  108.     }  
  109.     return true;  
  110.     }  
  111.   
  112.     /** 
  113.      * 获取关注的贴吧 
  114.      *  
  115.      * @return 
  116.      */  
  117.     private boolean getLikeBars() {  
  118.     print("getLikeBars...");  
  119.     String indexresult = getWebContent(mIndexUrl);  
  120.     if (indexresult == null)  
  121.         return false;  
  122.     Document document = Jsoup.parse(indexresult);  
  123.     Elements likebars = document.select("div.my_love_bar a");  
  124.     for (Element e : likebars) {  
  125.         mLikeBarsUrls.add(mUrlHead + e.attr("href"));  
  126.         mLikeBars.add(e.text());  
  127.     }  
  128.     if (mLikeBars.size() == 0)  
  129.         return false;  
  130.     return true;  
  131.     }  
  132.   
  133.     /** 
  134.      * 获取网页内容 
  135.      *  
  136.      * @param url 
  137.      *            链接地址 
  138.      * @return 网页内容 
  139.      */  
  140.     private String getWebContent(String url) {  
  141.     print("getWebContent...");  
  142.     String result = null;  
  143.     try {  
  144.         result = httpUtil.get(url);  
  145.     } catch (Exception e) {  
  146.         e.printStackTrace();  
  147.         return null;  
  148.     }  
  149.     return result;  
  150.     }  
  151.   
  152.     public void print(String s) {  
  153.     System.out.println(s);  
  154.     }  
  155. }  
  包含main方法的主类:

[java]   view plain copy
  1. package com.jingchen.main;  
  2.   
  3. import com.jingchen.util.BaiduSignUp;  
  4. import com.jingchen.util.SignUpTool;  
  5.   
  6. public class MainClass {  
  7.     public static String username = "******";  
  8.     public static String passwd = "******";  
  9.     public static int mInterval = 30;  
  10.   
  11.     public static void main(String[] args) {  
  12.     boolean isSignup = false;  
  13.     boolean isLogin = false;  
  14.     SignUpTool tool = new BaiduSignUp();  
  15.     // SignUpTool tool = new BBSLogin();  
  16.     while (true) {  
  17.         try {  
  18.         isLogin = tool.login(username, passwd);  
  19.         isSignup = tool.signUp();  
  20.         if (isLogin && isSignup) {  
  21.             // 签到成功则三小时后再签到  
  22.             System.out.println("continue after three hours...");  
  23.             Thread.sleep(3 * 60 * 60 * 1000);  
  24.         } else {  
  25.             // 签到失败则30分钟后再次签到  
  26.             System.out.println("continue after " + mInterval  
  27.                 + " minites...");  
  28.             Thread.sleep(mInterval * 60 * 1000);  
  29.         }  
  30.         } catch (Exception e) {  
  31.         e.printStackTrace();  
  32.         }  
  33.     }  
  34.     }  
  35.   
  36. }  
   每次签到前都先登录,签到失败则等半小时再尝试,签到成功则等待3小时再次签到。将用户名和密码填上就可以了。

就这么多了,不保证百度后台升级后程序会一直有效。


源码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值