好吧,这是我出差回来没任务的时候写的,当时挺无聊的。个人又比较喜欢爬虫这类脚本,写出来纯属玩玩,大家可以参考参考
功能:登录豆瓣,发表说说。
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 = "3094759846@qq.com"; //登录账号
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