package cn.hb.util;

 import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.interactions.Actions; /**
* 爬取说说写入到txt中,爬取100条
*
* @author tele
*
*/
public class QZTwitterCrawler {
static String url = "https://user.qzone.qq.com/1350560858";
static int maxSize = ;
static int pageSize = ;
static String userName="qq";
static String pwd = "密码";
public static void main(String[] args) throws InterruptedException, IOException {
login();
} /**
* 登录
*
* @throws InterruptedException
* @throws IOException
*/
public static void login() throws InterruptedException, IOException {
System.setProperty("webdriver.gecko.driver", "D:/browserdriver/geckodriver.exe"); FirefoxOptions options = new FirefoxOptions();
options.setBinary("F:/ff/firefox.exe"); WebDriver driver = new FirefoxDriver(options);
driver.manage().window().maximize();
// 超时
try {
driver.manage().timeouts().pageLoadTimeout(, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(, TimeUnit.SECONDS);
driver.get(url);
} catch (Exception e) {
System.out.println("所需元素已出现,停止加载页面");
} finally {
// 切换到登录login
driver.switchTo().frame("login_frame"); WebElement switcher_plogin = driver.findElement(By.id("switcher_plogin"));
System.out.println(switcher_plogin.getText());
if (switcher_plogin.isDisplayed()) {
switcher_plogin.click();
}
// 用户名
driver.findElement(By.id("u")).clear();
driver.findElement(By.id("u")).sendKeys(userName); // 密码
driver.findElement(By.id("p")).clear();
driver.findElement(By.id("p")).sendKeys(pwd); // 登录
try {
driver.findElement(By.id("login_button")).click();
Thread.sleep();
} catch (Exception e) {
e.printStackTrace();
} finally {
if ("https://i.qq.com/".equals(driver.getCurrentUrl())) {
System.out.println("登录失败!5秒后再次尝试登录");
Thread.sleep();
driver.findElement(By.id("login_button")).click();
}
} // 退出frame
driver.switchTo().defaultContent(); System.out.println(driver.getCurrentUrl()); JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
// 如果有亲密度提示 try { WebElement fs_guide = driver.findElement(By.xpath(
"//div[@id='friendship_promote_layer']/table[@class='tbl-fs-guide']//a"
)); if(fs_guide != null && fs_guide.isDisplayed()) {
fs_guide.click(); } } catch (Exception e) { e.printStackTrace();
}finally { } // 点击说说
driver.findElement(By.cssSelector("#menuContainer ul.head-nav-menu>li.menu_item_311>a")).click(); Thread.sleep(); // 切换到frame
driver.switchTo().frame(driver.findElement(By.className("app_canvas_frame"))); Thread.sleep(); // 拼接cookie
/* StringBuilder builder = new StringBuilder();
Set<Cookie> cookieSet = driver.manage().getCookies();
cookieSet.forEach(c -> builder.append(c.getName()).append("=").append(c.getValue()).append("; "));
cookies = builder.toString();*/ // 保存
saveTwitter(driver); System.out.println("内容提取完毕,退出浏览器");
driver.quit(); }
} /**
* 序列化
* @param driver
* @return
* @throws InterruptedException
* @throws IOException
*/
public static void saveTwitter(WebDriver driver) throws InterruptedException, IOException {
File file = new File("f:/qz/twitter.txt"); // 文件夹检测
if (!file.getParentFile().exists()) {
file.mkdirs();
} else {
file.delete();
} FileWriter fileWriter = new FileWriter(file, true); String xpath;
// 模拟按键进行滚动
Actions actions = new Actions(driver); //说说总量
String totalNumStr = driver.findElement(By.xpath("//div[@class='feed_num']/a")).getText();
int totalNum = Integer.parseInt(totalNumStr); // 计算页数
int totalPage = (int) Math.ceil((double)Math.min(maxSize, totalNum) / (double) pageSize); // 构造xpath
for (int i = ; i < totalPage; i++) { for (int j = ; j < pageSize; j++) {
xpath = "//ol[@id='msgList']/li[" + (j + ) + "]/div[3]/div[2]/pre[@class='content']";
// 获取说说内容
try {
WebElement element = driver.findElement(By.xpath(xpath));
String text = element.getText();
System.out.println("本页第" + (j + ) + "条 :" + text);
fileWriter.write(text, , text.length()); } catch (Exception e) {
e.printStackTrace();
} finally { }
if (j % == ) {
actions.sendKeys(Keys.ARROW_DOWN).perform();
}
}
System.out.println("第" + (i + ) + "页说说爬取完毕");
// 分页
if ((i + ) <= totalPage) {
driver.findElement(By.xpath("//a[@id='pager_num_" + i + "_" + (i + ) + "']")).click();
// 等待页面加载
Thread.sleep();
}
} if (fileWriter != null) {
fileWriter.close();
}
} }

比爬取相册简单点,唯一有点弯的是页码的构造了,我写的这个只支持获取文字,可以用来生成词云

selenium 爬取空间说说的更多相关文章

  1. 爬虫学习06用selenium爬取空间

    用selenium爬取空间 from selenium import webdriver from lxml import etree import time pro = webdriver.Chro ...

  2. webcollector + selenium 爬取空间相册图片

    package cn.hb.util; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWr ...

  3. [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)

    转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...

  4. selenium爬取煎蛋网

    selenium爬取煎蛋网 直接上代码 from selenium import webdriver from selenium.webdriver.support.ui import WebDriv ...

  5. 利用selenium爬取京东商品信息存放到mongodb

    利用selenium爬取京东商城的商品信息思路: 1.首先进入京东的搜索页面,分析搜索页面信息可以得到路由结构 2.根据页面信息可以看到京东在搜索页面使用了懒加载,所以为了解决这个问题,使用递归.等待 ...

  6. 利用Selenium爬取淘宝商品信息

    一.  Selenium和PhantomJS介绍 Selenium是一个用于Web应用程序测试的工具,Selenium直接运行在浏览器中,就像真正的用户在操作一样.由于这个性质,Selenium也是一 ...

  7. Scrapy 框架 使用 selenium 爬取动态加载内容

    使用 selenium 爬取动态加载内容 开启中间件 DOWNLOADER_MIDDLEWARES = { 'wangyiPro.middlewares.WangyiproDownloaderMidd ...

  8. 使用selenium爬取网站动态数据

    处理页面动态加载的爬取 selenium selenium是python的一个第三方库,可以实现让浏览器完成自动化的操作,比如说点击按钮拖动滚轮等 环境搭建: 安装:pip install selen ...

  9. scrapy框架 + selenium 爬取豆瓣电影top250......

    废话不说,直接上代码..... 目录结构 items.py import scrapy class DoubanCrawlerItem(scrapy.Item): # 电影名称 movieName = ...

随机推荐

  1. PatentTips - Maintaining shadow page tables in a sequestered memory region

    BACKGROUND Malicious code, known as malware, which includes viruses, worms, adware, etc., may attack ...

  2. noi25 最长最短单词(为什么会出现运行时错误)

    noi25 最长最短单词(为什么会出现运行时错误) 一.总结 一句话总结:比如除以零,数组越界,指针越界,使用已经释放的空间,数组开得太大,超出了栈的范围,造成栈溢出 1.c++报runtime er ...

  3. 什么是MVC,什么是WCF

    在C#中总会遇到这几个概念,网上搜了一下,做一下总结和比较,东拼西凑,如有雷同,纯属直接拷贝,人懒,但无意侵权. 1.什么是MVC MVC是三个单词的首字母缩写,它们是Model(模型).View(视 ...

  4. javascript进阶课程--第三章--匿名函数和闭包

    javascript进阶课程--第三章--匿名函数和闭包 一.总结 二.学习要点 掌握匿名函数和闭包的应用 三.匿名函数和闭包 匿名函数 没有函数名字的函数 单独的匿名函数是无法运行和调用的 可以把匿 ...

  5. ZOJ 1489 2^x mod n = 1 数论

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=489 题目大意: 给你正整数n,求最小的x使得2^x mod n = 1. 思路 ...

  6. 使用boost::property_tree生成带attribute的xml

    曾经写过一篇"使用Boost property tree来解析带attribute的xml", 但是还有姐妹篇一直没贴.看看前一篇贴了都快都快3年了,时间过的真快. 这一小篇就算是 ...

  7. js实现第一次打开网页弹出指定窗口(常用功能封装很好用)

    js实现第一次打开网页弹出指定窗口(常用功能封装很好用) 一.总结 1.常用功能封装:之前封装的cookie的操作函数非常好用,我自己也可以这么搞 二.js实现第一次打开网页弹出指定窗口 练习1:第一 ...

  8. HDMI ARC功能详解及应用介绍

    http://www.icpcw.com/Parts/Peripheral/Skill/3260/326044_2.htm [电脑报在线]很多用户和读者购买了电视以后,都发现自己电视的HDMI接口上经 ...

  9. 18.1 IIC驱动程序(基于3.4.2内核)

    驱动使用smbus提供的IIC读写函数可以参考smbus-protocol.txt文档:应用层直接使用IIC读写函数读写IIC设备,应用层读写函数是由i2c-tools这个库提供的(编译的使用和应用程 ...

  10. NASM Syntax

    NASM has a simplified syntax designed to let the user code with minimum overhead. In its simplest fo ...