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. HTML学习笔记——frameset和marquee

    1>frameset 效果如百Google度 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  2. FCC上的初级算法题

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

  3. js正则,电话,邮箱

    1. <script type="text/javascript"> var str="Is this all th05777-89856825ere is5 ...

  4. JavaScript中原型和原型链

    原型[prototype]: 为其他对象提供共享属性的对象. 每个函数都有一个原型(prototype)属性,这个属性是一个指针,指向一个对象,这个对象包含特定实例共享的一些属性和方法. 以例服人: ...

  5. 两个select转移

    <table> <tr> <td> <select multiple="multiple" id="leftSelect&quo ...

  6. asp.net mvc中在使用async的时候HttpContext为null的问题

    摘要 HttpContext上下文并不是无处不在的.详情可以看下Fish Li的文章,解释的比较清楚. HttpContext.Current并非无处不在 问题复现 public async Task ...

  7. asp.net core 中的MD5加密

    尝试了很长时间,但是一直报core 5 不可用,当时就崩溃了. 但是偶然的机会 我添加了Microsoft.AspNet.Identity 之后.MD5就好用了. 估计是这个报实现了core5下的MD ...

  8. 工作中一个简单的shell程序

    下面是工作中用到的链接数据库的shell程序. #!/bin/bash ] ; then echo "prase is wrong ,please check first" exi ...

  9. ZOJ3802 Easy 2048 Again (状压DP)

    ZOJ Monthly, August 2014 E题 ZOJ月赛 2014年8月 E题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...

  10. svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted

    svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted (2014-08-1 ...