好吧,這是我出差回來沒任務的時候寫的,當時挺無聊的。個人又比較喜歡爬蟲這類腳本,寫出來純屬玩玩,大家可以參考參考
功能:登錄豆瓣,發表說說。
package douban;
import java.awt.FlowLayout;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.json.JSONObject;
/**
* 上午無聊寫的豆瓣網登錄測試類
* 使用方式:直接執行即可,或只修改static裏的參數,其它地方不需要修改
* 參考範圍:爬蟲新手,大神莫噴
* @author 默非默
*
*/
public class DouBan {
static String loginName = "[email protected]"; //登錄賬號
static String password = "520xiao707789"; //登錄密碼
static String username = "默非默"; // 用戶暱稱(非登錄名),根據用戶名稱來判斷是否登錄成功,可以是名稱中的一部分,如:默非默2016,可以寫默非默即可
public static void main(String[] args) throws Exception {
// 設置登錄頁面,用來獲取Cookie和得到驗證碼地址及token
String index_1 = "https://www.douban.com/j/misc/captcha";
// 設置Cookie管理器
CookieManager manager = new CookieManager();
CookieHandler.setDefault(manager);
URL url = new URL(index_1);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream in = connection.getInputStream();
byte[] data = new byte[in.available()];
in.read(data);
String html1 = new String(data);
connection.disconnect();
// 從上一地址返回內容中獲取驗證碼鏈接
JSONObject json = new JSONObject(html1);
String YZMurlstr = json.getString("url");
// 連接驗證碼地址
URL YZMurl = new URL("https:" + YZMurlstr);
HttpURLConnection conn = (HttpURLConnection) YZMurl.openConnection();
InputStream in2 = YZMurl.openConnection().getInputStream();
// 控制檯輸入驗證碼,並返回。
String inCode = outCode(in2);
conn.disconnect();
// 開始登錄
URL logUrl = new URL("https://accounts.douban.com/login");
conn = (HttpURLConnection) logUrl.openConnection();
conn.setRequestMethod("POST");// 提交模式
conn.setDoOutput(true);// 是否輸入參數
Map<String, String> map = new HashMap<String, String>();
map.put("source", "None");
map.put("redir", "https://www.douban.com/");
map.put("form_email", loginName);
map.put("form_password", password);
map.put("captcha-solution", inCode);
map.put("captcha-id", json.getString("token"));
map.put("login", "登錄");
String parames = map.toString().replaceAll(", ", "&").replace("{", "").replace("}", "");
conn.getOutputStream().write(parames.getBytes()); // 發送參數到服務器
InputStream inStream = conn.getInputStream(); // 提交併返回輸出流
System.out.println(conn.getResponseCode());
if (conn.getResponseCode() == 302) {
String indexUrl = conn.getHeaderField("Location");
conn.disconnect();
conn = (HttpURLConnection) new URL(indexUrl).openConnection();
String body = OutHtml(conn.getInputStream());
if (body.contains(username)) {
System.out.println("恭喜" + username + ",登錄成功302");
}
}
if (conn.getResponseCode() == 200) {
String body = OutHtml(inStream);
if (body.contains(username)) {
System.out.println("恭喜" + username + ",登錄成功200");
}
}
// 打印Cookie
outCookie(manager);
conn.disconnect();
while (true){
System.out.print("輸入你想發表的說說:");
Scanner scr = new Scanner(System.in);
String conment = scr.nextLine();
// 登錄成功後,發表說說
conn = (HttpURLConnection) new URL("https://www.douban.com/")
.openConnection();
conn.setRequestMethod("POST");// 提交模式
conn.setDoOutput(true);// 是否輸入參數
String sayParames = "ck=fGh2&comment=" + conment; // 構建表單
conn.getOutputStream().write(sayParames.getBytes()); // 輸入參數
InputStream sayInput = conn.getInputStream(); // 提交
System.out.println(conn.getResponseCode());
if (conn.getResponseCode() == 302) {
String sayUrl2 = conn.getHeaderField("Location");
conn.disconnect();
conn = (HttpURLConnection) new URL(sayUrl2).openConnection();
String body2 = OutHtml(conn.getInputStream());
if (body2.contains(conment)) {
System.out.println(conment + " 發表成功");
} else {
System.out.println("發表失敗");
}
}
if (conn.getResponseCode() == 200) {
String body2 = OutHtml(sayInput);
if (body2.contains(conment)) {
System.out.println(conment + " 發表成功");
} else {
System.out.println("發表失敗");
}
}
}
}
// 得到並彈窗顯示驗證碼,以及在控制檯輸入
private static String outCode(InputStream in) {
try {
FileOutputStream baos = new FileOutputStream(new File("d:/YZM.jpg"));
byte[] image = new byte[in.available()];
in.read(image);
baos.write(image);
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
frame.setVisible(false);
frame.setBounds(300, 100, 300, 100);
frame.setLayout(new FlowLayout());
ImageIcon icon = new ImageIcon("D:/YZM.jpg");
frame.add(new JLabel(icon));
frame.setVisible(true);
System.out.print("輸入你看到的驗證碼:");
Scanner scr = new Scanner(System.in);
String incode = scr.nextLine();
System.out.println("驗證碼 : " + incode);
frame.dispose();
return incode;
}
// 打印Cookie
private static void outCookie(CookieManager manager) {
// 得到返回的cookie內容
CookieStore cookieJar = manager.getCookieStore();
List<HttpCookie> cookies = cookieJar.getCookies();
for (HttpCookie cookie : cookies) {
System.out.println(cookie);
}
}
// 打印網頁內容
private static String OutHtml(InputStream in) {
StringBuffer result = new StringBuffer();
try {
InputStreamReader inr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(inr);
String line;
while ((line = br.readLine()) != null) {
result.append(line + "<br>");
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
}
坑爹啊,csdn上傳的2個包暫時顯示不了,分別是json和jsoup包。
上傳百度網盤了:http://pan.baidu.com/s/1qYCfPKw