花了兩天時間,搞了個豆瓣自動回帖的程序。。。

原理不難,就是http client和 htmlParse的東西。html

豆瓣爲了防止惡意發貼,在回覆或者發新貼的時候,有時候須要驗證碼,這個驗證碼還不太好識別(若是有高手,請聯繫我!),不過,我發現了一個程序上的漏洞,能夠繞過去。java

先打開IE或者其餘瀏覽器進行登陸。node

 

貼上部分代碼,僅供學習參考:apache

import java.util.ArrayList;
import java.util.List;
import java.util.Random;瀏覽器

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
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.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;安全

public class DoubanCCSUtils {app

public static void main(String[] args) {
comment();
}dom

public static void comment() {學習

// String httpUrl = "http://www.douban.com/group/M-P/new_topic";
String httpUrl = "http://www.douban.com/group/";   //個人小組裏的最新貼子,只取第一頁
// HttpPost鏈接對象
HttpGet httpGet = new HttpGet(httpUrl);url

httpGet.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
// httpGet.addHeader("Accept-Encoding","gzip,deflate,sdch"); 不能壓縮,不然亂碼,壓縮須要瀏覽器支持
httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
httpGet.addHeader("Cache-Control", "max-age=0");
httpGet.addHeader("Connection", "keep-alive");
httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
// ck,dbcly這兩個參數會變化
httpGet.addHeader(
"Cookie","");  //Cookie,本身查瀏覽器
httpGet.addHeader("Host", "www.douban.com");
httpGet.addHeader("Origin", "http://www.douban.com");
httpGet.addHeader("Referer", "http://www.douban.com/group/");
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36");

httpGet.addHeader("Content-type", "text/html; charset=utf-8");
// 設置字符集
try {
// 取得默認的HttpClient
HttpClient httpclient = new DefaultHttpClient();

// 取得HttpResponse
HttpResponse httpResponse = httpclient.execute(httpGet);
// HttpStatus.SC_OK表示鏈接成功
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得返回的字符串
String strResult = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);
Parser parser = new Parser(strResult);
NodeFilter filter = new TagNameFilter("A");
NodeList nodes = parser.extractAllNodesThatMatch(filter);
if (nodes != null) {
for (int i = 0; i < nodes.size(); i++) {
Node textnode = (Node) nodes.elementAt(i);
String s = textnode.getText();
if (s.contains("http://www.douban.com/group/topic") && s.contains("title=")) {
s = getTopicUrl(s);
addComment(s);
}

}
}
System.out.println("完成!");
}

} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
}

public static String getTopicUrl(String text) {
text = text.replace("a href=", "");
text = text.replace("\"", "");
String[] arr = text.split(" ");

return arr[0];
}

public static void addComment(String httpUrl) {

System.out.println(httpUrl);
httpUrl = httpUrl + "add_comment#last";
// HttpPost鏈接對象
HttpPost httpPost = new HttpPost(httpUrl);

httpPost.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpPost.addHeader("Accept-Encoding", "gzip,deflate,sdch");
httpPost.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
httpPost.addHeader("Cache-Control", "max-age=0");
httpPost.addHeader("Connection", "keep-alive");
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
// ck,dbcly這兩個參數會變化
httpPost.addHeader(
"Cookie","");  //這個Cookie,本身根據瀏覽器去查吧
httpPost.addHeader("Host", "www.douban.com");
httpPost.addHeader("Origin", "http://www.douban.com");
httpPost.addHeader("Referer", "http://www.douban.com/group/");
httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36");

// 使用NameValuePair來保存要傳遞的Post參數
List<NameValuePair> params = new ArrayList<NameValuePair>();

// 添加要傳遞的參數
params.add(new BasicNameValuePair("ck", "VdIW"));
params.add(new BasicNameValuePair("rv_comment", getMyComment()));
params.add(new BasicNameValuePair("start", "0"));
params.add(new BasicNameValuePair("submit_btn", "加上去"));

//params.add(new BasicNameValuePair("captcha-solution", "monkey"));
//params.add(new BasicNameValuePair("captcha-id", "YTYPMnsapAJsXw0o2w6T5SY5"));

// 設置字符集
try {
HttpEntity httpentity = new UrlEncodedFormEntity(params, "utf-8");
// 請求httpPost
httpPost.setEntity(httpentity);
// 取得默認的HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 取得HttpResponse
HttpResponse httpResponse = httpclient.execute(httpPost);
int status = httpResponse.getStatusLine().getStatusCode();
System.out.println(status);
if (status == 200) { //200實際意味着失敗,須要驗證碼
DoubanVCUtils.getDoubanVC();  //解決驗證碼問題
}
if (status == 302) { //302轉向意味着成功了

}

Thread.currentThread().sleep(5000); // 設置暫停毫秒,防止引發豆瓣注意, 這個時間可長可短,根據須要

} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
}

public static String getMyComment() {
String[] comments = new String[20];
comments[0] = "幫頂一下。中國兒童安全網,關注兒童安全每一天!";
comments[1] = "支持樓主!中國兒童安全網,關注兒童安全每一天!";
comments[2] = "佔個坑!中國兒童安全網,關注兒童安全每一天!";
省略部分。。。。

Random r = new Random();
int k = r.nextInt(20);

String s = "abcdefghijklmnopqrstuvwxyz"; char t[] = new char[26]; for (int x = 0; x < 26; x++) { t[x] = s.charAt(x); } int j = r.nextInt(26); return comments[k] + t[j];   //防止內容重複,豆瓣有檢查機制 }}

相關文章
相關標籤/搜索