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. 兼容IE的写法收集||bug修复

    这篇文章实时更新 属于IE的专属写法 其中,S表示Standards Mode即标准模式,Q表示Quirks Mode,即兼容模式 hack 示例 IE6(S) IE6(Q) IE7(S) IE7(Q ...

  2. php引用计数的基本知识

    每个php变量存在一个叫"zval"的变量容器中.一个zval变量容器,除了包含变量的类型和值,还包括两个字节的额外信息.第一个是"is_ref",是个bool ...

  3. 彻底解决Eclipse自动补全变量名及变量名后面追加类型名

    彻底解决Eclipse自动补全变量名问题的方法步骤 发布于 2014-11-04 14:53   已被阅读 31613159 次 大家使用eclipse或者MyEclipse敲代码的时候,是不是都被这 ...

  4. IE安全分析

    IE安全问题,这个话题似乎很古老了,但是问题又是层出不穷~ 对于IE的安全,我个人认为有两点需要关注,一个是上网的安全,二个是IE解析代码的安全 对于IE上网安全,这是最基本的,也是最常用的了 附上一 ...

  5. MVC 随记

    2014-09-04 [1] Json var contact = new Object(); contact.firstname = "Jesper"; contact.surn ...

  6. 将excel文件中的数据导入到mysql

    ·在你的表格中增加一列,利用excel的公式自动生成sql语句,具体方法如下:          1)增加一列(假设是D列)          2)在第一行的D列,就是D1中输入公式:=CONCATE ...

  7. asp.net mvc 实现博客的时间分类管理

    先看效果 这个其实用c#实现起来比较简单: Sides = bllSession.IArticleBLL.GetList("") .Select(a => a.Time) . ...

  8. spring配置文件详解--真的蛮详细

    spring配置文件详解--真的蛮详细   转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常 ...

  9. hdu4751 Divide Groups

    This year is the 60th anniversary of NJUST, and to make the celebration more colorful, Tom200 is goi ...

  10. HttpClient连接池的连接保持、超时和失效机制

    HTTP是一种无连接的事务协议,底层使用的还是TCP,连接池复用的就是TCP连接,目的就是在一个TCP连接上进行多次的HTTP请求从而提高性能.每次HTTP请求结束的时候,HttpClient会判断连 ...