import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit; import org.apache.http.client.CookieStore;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait; public class GetMultiCookieMSinaTimely { public static void main(String[] args) throws Exception {
/*if(args.length == 3) {
GetMultiCookieMSinaTimely.cookieFile = args[0];
GetMultiCookieMSinaTimely.chromedriverPath = args[1];
GetMultiCookieMSinaTimely.cookieSavePath = args[2];
} else {
System.out.println(new Date() + "three args:cookieFile path,chromedriver path,cookie save path");
System.exit(0);
}*/
GetMultiCookieMSinaTimely.cookieFile = "E:\\学习\\微博爬虫\\cookie\\user.txt";
GetMultiCookieMSinaTimely.chromedriverPath = "E:\\学习\\微博爬虫\\cookie\\chromedriver.exe";
GetMultiCookieMSinaTimely.cookieSavePath = "E:\\学习\\微博爬虫\\cookie\\";
if(new File(GetMultiCookieMSinaTimely.cookieFile).isFile()) {
GetMultiCookieMSinaTimely getCookies = new GetMultiCookieMSinaTimely();
getCookies.getCookie();
} else {
System.err.println(new Date() + "args is not a file!");
} } private static String cookieFile;
private static String chromedriverPath;
private static String cookieSavePath;
private int cookieNumber;
public GetMultiCookieMSinaTimely() {
} public void getCookie() throws IOException, InterruptedException {
if(cookieFile == null || cookieFile.equals("")) {
System.err.println(new Date() + "no emailpw File!");
return;
}
cookieNumber=0;
File loginFile = new File(cookieFile);
FileInputStream fis=new FileInputStream(loginFile);
InputStreamReader isr=new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String temp; ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
System.setProperty("webdriver.chrome.driver", chromedriverPath); // ChromeDriverService service = new ChromeDriverService.Builder()
// .usingDriverExecutable(new File(chromedriverPath)).usingAnyFreePort().build();
// service.start(); List<String> uselessList = new LinkedList<String>(); while((temp = br.readLine()) != null) {
oneCookie(temp, uselessList);
}
List<String> tempUselessList = new LinkedList<String>();
while(uselessList.size()>0){
tempUselessList.clear();
for(String s:uselessList) {
oneCookie(s, tempUselessList);
}
uselessList.clear();
uselessList.addAll(tempUselessList);
}
br.close();
// 关闭 ChromeDriver 接口
// service.stop();
// System.out.println("ok!");
} private void oneCookie(String temp, List<String> uselessList) {
try {
String[] username_password = temp.split("\\|");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
System.out.println(new Date() + "logging... "+ temp);
driver.get("https://passport.weibo.cn/signin/login"); try {
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElement(By.id("loginName")) != null;
}
});
}catch(TimeoutException e) {
System.out.println(new Date() + "TimeoutException,add "+temp+" to uselessList!");
uselessList.add(temp);
driver.quit();
return;
}
Thread.sleep(1500); WebElement username = driver.findElement(By.id("loginName"));
username.click();
username.clear();
username.sendKeys(username_password[1]);
WebElement password = driver.findElement(By.id("loginPassword"));
password.click();
password.clear();
password.sendKeys(username_password[2]);
WebElement submit = driver.findElement(By.id("loginAction"));
submit.click();
try {
(new WebDriverWait(driver, 5)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("微博");
}
});
}catch(TimeoutException e) {
System.out.println(new Date() + "useless account,add "+temp+" to uselessList!");
uselessList.add(temp);
driver.quit();
return;
}
// System.out.println(cookieNumber);
// System.out.println("success");
Set<Cookie> allCookies = driver.manage().getCookies(); try {
CookieStore cookiestore = new BasicCookieStore();
for (@SuppressWarnings("rawtypes")
Iterator iterator = allCookies.iterator(); iterator.hasNext();) {
Cookie cookie = (Cookie) iterator.next();
BasicClientCookie bcookie = new BasicClientCookie(cookie.getName(), cookie.getValue());
bcookie.setDomain(cookie.getDomain());
bcookie.setExpiryDate(cookie.getExpiry());
bcookie.setPath(cookie.getPath());
cookiestore.addCookie(bcookie);
} new File(cookieSavePath).mkdirs();
//FileWriter fWriter=null;
//BufferedWriter bw=null;
//PrintWriter pWriter=null;
File file = new File(cookieSavePath+"/cookie.file" + cookieNumber++);
//fWriter =new FileWriter(cookieSavePath+"/cookie"+cookieNumber++);
//bw=new BufferedWriter(fWriter);
//pWriter=new PrintWriter(bw);
//pWriter.write(cookiestore.toString());
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
System.out.println("cookile:"+cookiestore);
oos.writeObject(cookiestore);
oos.close();
fos.close();
} catch (IOException e) {
System.out.println("IOException,add "+temp+" to uselessList!");
uselessList.add(temp);
}
driver.quit();
} catch(Exception e) {
e.printStackTrace();
System.out.println("Exception,add "+temp+" to uselessList!");
uselessList.add(temp);
return;
}
System.out.println(new Date() + "logging succeed! "+ temp);
} }

Java获取新浪微博cookies的更多相关文章

  1. js/java 获取、添加、修改、删除cookie(最全)

      一.cookie介绍 1.cookie的本来面目 HTTP协议本身是无状态的.什么是无状态呢,即服务器无法判断用户身份.Cookie实际上是一小段的文本信息(key-value格式).客户端向服务 ...

  2. java获取https网站证书,附带调用https:webservice接口

    一.java 获取https网站证书: 1.创建一个java工程,新建InstallCert类,将以下代码复制进去 package com; import java.io.BufferedReader ...

  3. java获取图片原始尺寸

    java获取图片原始尺寸 URL url = null; InputStream is = null; BufferedImage img = null; try { url = new URL(pi ...

  4. java学习第13天( java获取当前时间,有关大数据的运算及精确数字运算,Date类)

    一 java获取当前时间 学习一个函数,得到当前时间的准确值 System.currectTimeMillis(). 可以得到以毫秒为单位的当前时间.它主要用于计算程序运行时间,long start= ...

  5. Java获取Web服务器文件

    Java获取Web服务器文件 如果获取的是服务器上某个目录下的有关文件,就相对比较容易,可以设定死绝对目录,但是如果不能设定死绝对目录,也不确定web服务器的安装目录,可以考虑如下两种方式: 方法一: ...

  6. 【java 获取数据库信息】获取MySQL或其他数据库的详细信息

    1.首先是 通过数据库获取数据表的详细列信息 package com.sxd.mysqlInfo.test; import java.sql.Connection; import java.sql.D ...

  7. java获取获得Timestamp类型的当前系统时间。以及java.util.date 、java.sql.Date之间的转换

    java获取取得Timestamp类型的当前系统时间java获取取得Timestamp类型的当前系统时间 格式:2010-11-04 16:19:42 方法1: Timestamp d = new T ...

  8. spring java 获取webapp下文件路径

    spring java 获取webapp下文件路径 @RequestMapping("/act/worldcup_schedule_time/imgdownload") @Resp ...

  9. java 获取classpath下文件多种方式

    java 获取classpath下文件多种方式 一:properties下配置 在resources下定义server.properties register.jks.path=classpath\: ...

随机推荐

  1. FCC上的初级算法题

    核心提示:FCC的算法题一共16道.跟之前简单到令人发指的基础题目相比,难度是上了一个台阶.主要涉及初步的字符串,数组等运算.仍然属于基础的基础,官方网站给出的建议完成时间为50小时,超出了之前所有非 ...

  2. 第4章 jQuery的事件和动画(二)

    二. jQuery中的动画 动画在前面几章案例中是回避不了的问题.此处结合一些简便的写法稍作系统的分析. 1. show()和hide()(1)介绍——不用过多的介绍了jQuery最基本的方法.本质是 ...

  3. Lua 之 userdata

    Lua 之 userdata 在Lua中可以通过自定义类型(user data)与C语言代码更高效.更灵活的交互,从而扩展Lua能够表达的类型. full userdata full userdata ...

  4. OC-基本

    #import <Foundation/NSObjCRuntime.h> //import: //1,同#include一样, 拷贝文件内容 //2,可以自动防止文件的内容被重复拷贝 /* ...

  5. [webgrid] – selecterow - (Get Selected Row from ASP.NET MVC 3 WebGrid)

    Get Selected Row from ASP.NET MVC 3 WebGrid Abstract: The following article demonstrates how to get ...

  6. p命名空间的使用(不推荐用)

    xmlns:p="http://www.springframework.org/schema/p" p:没有xsd文件,直接加上面那句就好了 <!-- singleton和p ...

  7. C# 协变out 、逆变 in

    需求:泛型使用多态性 备注:协变逆变只能修饰 接口和委托 简单理解: 1.使用 in 修饰后为逆变,只能用作形参使用 ,参考 public delegate void Action<in T&g ...

  8. clearfix--清除浮动

    .clearfix { zoom: ; display: table; width: %; } .clearfix:after { content: " "; display: b ...

  9. FZU2169 shadow题解

    http://acm.fzu.edu.cn/problem.php?pid=2169 Problem Description YL 是shadow国的国王,shadow国有N个城市.为了节省开支,sh ...

  10. MFC CStatic类动态创建

    如果我使用下面风格: m_PictureCtrl.Create(NULL, WS_EX_TRANSPARENT|WS_CHILD|WS_VISIBLE|WS_TABSTOP|SS_OWNERDRAW, ...