Java获取新浪微博cookies
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的更多相关文章
- js/java 获取、添加、修改、删除cookie(最全)
一.cookie介绍 1.cookie的本来面目 HTTP协议本身是无状态的.什么是无状态呢,即服务器无法判断用户身份.Cookie实际上是一小段的文本信息(key-value格式).客户端向服务 ...
- java获取https网站证书,附带调用https:webservice接口
一.java 获取https网站证书: 1.创建一个java工程,新建InstallCert类,将以下代码复制进去 package com; import java.io.BufferedReader ...
- java获取图片原始尺寸
java获取图片原始尺寸 URL url = null; InputStream is = null; BufferedImage img = null; try { url = new URL(pi ...
- java学习第13天( java获取当前时间,有关大数据的运算及精确数字运算,Date类)
一 java获取当前时间 学习一个函数,得到当前时间的准确值 System.currectTimeMillis(). 可以得到以毫秒为单位的当前时间.它主要用于计算程序运行时间,long start= ...
- Java获取Web服务器文件
Java获取Web服务器文件 如果获取的是服务器上某个目录下的有关文件,就相对比较容易,可以设定死绝对目录,但是如果不能设定死绝对目录,也不确定web服务器的安装目录,可以考虑如下两种方式: 方法一: ...
- 【java 获取数据库信息】获取MySQL或其他数据库的详细信息
1.首先是 通过数据库获取数据表的详细列信息 package com.sxd.mysqlInfo.test; import java.sql.Connection; import java.sql.D ...
- java获取获得Timestamp类型的当前系统时间。以及java.util.date 、java.sql.Date之间的转换
java获取取得Timestamp类型的当前系统时间java获取取得Timestamp类型的当前系统时间 格式:2010-11-04 16:19:42 方法1: Timestamp d = new T ...
- spring java 获取webapp下文件路径
spring java 获取webapp下文件路径 @RequestMapping("/act/worldcup_schedule_time/imgdownload") @Resp ...
- java 获取classpath下文件多种方式
java 获取classpath下文件多种方式 一:properties下配置 在resources下定义server.properties register.jks.path=classpath\: ...
随机推荐
- Nginx:Pitfalls and Common Mistakes
New and old users alike can run into a pitfall. Below we outline issues that we see frequently as we ...
- yourphp点击刷新验证码
加入css <script type="text/javascript" src="./Public/Js/my.js"></script&g ...
- docker管理shipyard中文版v3.0.2更新
shipyard中文版v3.0.2更新 https://console.dockerclub.net/ https://dockerclub.net/docs/intro/getting_start ...
- PHP任意文件包含绕过截断新姿势
前言 此方法是@l3m0n叔叔给我分享的,原文已经发布在90sec 我没有90sec的账号,所以自己实践一下,顺道安利给访问我博客的小伙伴. 适用情况 可以控制协议的情况下,如果%00无法截断包含,可 ...
- Windows下安装Tomcat服务
startup.bat中添加以下内容 setlocal SET JAVA_HOME=D:\Program Files\Java\jdk1.8.0_05 SET CATALINA_HOME=D:\Pro ...
- jq实现鼠标移动到 图片上放大,移开图片缩小效果(打算封装成插件)
先看代码 <script> $(function() { $('div').mouseover(function() { $('img').animate({ opacity: '0.9' ...
- JQuery中的html(),text(),val()区别
jQuery中.html()用为读取和修改元素的HTML标签,.text()用来读取或修改元素的纯文本内容,.val()用来读取或修改表单元素的value值. 1.HTML html():取得第一个匹 ...
- 【AngularJS】—— 9 自定义过滤器
AngularJS另一个特点就是提供了过滤器,可以通过操作UNIX下管道的方式,操作数据结果. 通过使用管道,可以便于双向的数据绑定中视图的展现. 过滤器在处理过程中,将数据变成新的格式,而且可以使用 ...
- web api 处理发送过来的文件(图片)
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.We ...
- 【Solr】 solr对拼音搜索和拼音首字母搜索的支持
问:对于拼音和拼音首字母的支持,当你在搜商品的时候,如果想输入拼音和拼音首字母就给出商品的信息,怎么办呢? 实现方式有2种,但是他们其实是对应的. 用lucene实现 1.建索引, 多建一个索引字段 ...