1. package cn.hb.util;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.Set;
  7. import java.util.concurrent.TimeUnit;
  8. import org.openqa.selenium.By;
  9. import org.openqa.selenium.Cookie;
  10. import org.openqa.selenium.JavascriptExecutor;
  11. import org.openqa.selenium.Keys;
  12. import org.openqa.selenium.WebDriver;
  13. import org.openqa.selenium.WebElement;
  14. import org.openqa.selenium.firefox.FirefoxDriver;
  15. import org.openqa.selenium.firefox.FirefoxOptions;
  16. import org.openqa.selenium.interactions.Actions;
  17.  
  18. /**
  19. * 爬取说说写入到txt中,爬取100条
  20. *
  21. * @author tele
  22. *
  23. */
  24. public class QZTwitterCrawler {
  25. static String url = "https://user.qzone.qq.com/1350560858";
  26. static int maxSize = ;
  27. static int pageSize = ;
  28. static String userName="qq";
  29. static String pwd = "密码";
  30. public static void main(String[] args) throws InterruptedException, IOException {
  31. login();
  32. }
  33.  
  34. /**
  35. * 登录
  36. *
  37. * @throws InterruptedException
  38. * @throws IOException
  39. */
  40. public static void login() throws InterruptedException, IOException {
  41. System.setProperty("webdriver.gecko.driver", "D:/browserdriver/geckodriver.exe");
  42.  
  43. FirefoxOptions options = new FirefoxOptions();
  44. options.setBinary("F:/ff/firefox.exe");
  45.  
  46. WebDriver driver = new FirefoxDriver(options);
  47. driver.manage().window().maximize();
  48. // 超时
  49. try {
  50. driver.manage().timeouts().pageLoadTimeout(, TimeUnit.SECONDS);
  51. driver.manage().timeouts().setScriptTimeout(, TimeUnit.SECONDS);
  52. driver.get(url);
  53. } catch (Exception e) {
  54. System.out.println("所需元素已出现,停止加载页面");
  55. } finally {
  56. // 切换到登录login
  57. driver.switchTo().frame("login_frame");
  58.  
  59. WebElement switcher_plogin = driver.findElement(By.id("switcher_plogin"));
  60. System.out.println(switcher_plogin.getText());
  61. if (switcher_plogin.isDisplayed()) {
  62. switcher_plogin.click();
  63. }
  64. // 用户名
  65. driver.findElement(By.id("u")).clear();
  66. driver.findElement(By.id("u")).sendKeys(userName);
  67.  
  68. // 密码
  69. driver.findElement(By.id("p")).clear();
  70. driver.findElement(By.id("p")).sendKeys(pwd);
  71.  
  72. // 登录
  73. try {
  74. driver.findElement(By.id("login_button")).click();
  75. Thread.sleep();
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. } finally {
  79. if ("https://i.qq.com/".equals(driver.getCurrentUrl())) {
  80. System.out.println("登录失败!5秒后再次尝试登录");
  81. Thread.sleep();
  82. driver.findElement(By.id("login_button")).click();
  83. }
  84. }
  85.  
  86. // 退出frame
  87. driver.switchTo().defaultContent();
  88.  
  89. System.out.println(driver.getCurrentUrl());
  90.  
  91. JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
  92. // 如果有亲密度提示
  93.  
  94. try { WebElement fs_guide = driver.findElement(By.xpath(
  95. "//div[@id='friendship_promote_layer']/table[@class='tbl-fs-guide']//a"
  96. )); if(fs_guide != null && fs_guide.isDisplayed()) {
  97. fs_guide.click(); } } catch (Exception e) { e.printStackTrace();
  98. }finally {
  99.  
  100. }
  101.  
  102. // 点击说说
  103. driver.findElement(By.cssSelector("#menuContainer ul.head-nav-menu>li.menu_item_311>a")).click();
  104.  
  105. Thread.sleep();
  106.  
  107. // 切换到frame
  108. driver.switchTo().frame(driver.findElement(By.className("app_canvas_frame")));
  109.  
  110. Thread.sleep();
  111.  
  112. // 拼接cookie
  113. /* StringBuilder builder = new StringBuilder();
  114. Set<Cookie> cookieSet = driver.manage().getCookies();
  115. cookieSet.forEach(c -> builder.append(c.getName()).append("=").append(c.getValue()).append("; "));
  116. cookies = builder.toString();*/
  117.  
  118. // 保存
  119. saveTwitter(driver);
  120.  
  121. System.out.println("内容提取完毕,退出浏览器");
  122. driver.quit();
  123.  
  124. }
  125. }
  126.  
  127. /**
  128. * 序列化
  129. * @param driver
  130. * @return
  131. * @throws InterruptedException
  132. * @throws IOException
  133. */
  134. public static void saveTwitter(WebDriver driver) throws InterruptedException, IOException {
  135. File file = new File("f:/qz/twitter.txt");
  136.  
  137. // 文件夹检测
  138. if (!file.getParentFile().exists()) {
  139. file.mkdirs();
  140. } else {
  141. file.delete();
  142. }
  143.  
  144. FileWriter fileWriter = new FileWriter(file, true);
  145.  
  146. String xpath;
  147. // 模拟按键进行滚动
  148. Actions actions = new Actions(driver);
  149.  
  150. //说说总量
  151. String totalNumStr = driver.findElement(By.xpath("//div[@class='feed_num']/a")).getText();
  152. int totalNum = Integer.parseInt(totalNumStr);
  153.  
  154. // 计算页数
  155. int totalPage = (int) Math.ceil((double)Math.min(maxSize, totalNum) / (double) pageSize);
  156.  
  157. // 构造xpath
  158. for (int i = ; i < totalPage; i++) {
  159.  
  160. for (int j = ; j < pageSize; j++) {
  161. xpath = "//ol[@id='msgList']/li[" + (j + ) + "]/div[3]/div[2]/pre[@class='content']";
  162. // 获取说说内容
  163. try {
  164. WebElement element = driver.findElement(By.xpath(xpath));
  165. String text = element.getText();
  166. System.out.println("本页第" + (j + ) + "条 :" + text);
  167. fileWriter.write(text, , text.length());
  168.  
  169. } catch (Exception e) {
  170. e.printStackTrace();
  171. } finally {
  172.  
  173. }
  174. if (j % == ) {
  175. actions.sendKeys(Keys.ARROW_DOWN).perform();
  176. }
  177. }
  178. System.out.println("第" + (i + ) + "页说说爬取完毕");
  179. // 分页
  180. if ((i + ) <= totalPage) {
  181. driver.findElement(By.xpath("//a[@id='pager_num_" + i + "_" + (i + ) + "']")).click();
  182. // 等待页面加载
  183. Thread.sleep();
  184. }
  185. }
  186.  
  187. if (fileWriter != null) {
  188. fileWriter.close();
  189. }
  190. }
  191.  
  192. }

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

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. Java对ad操作

    转载:http://blog.csdn.net/binyao02123202/article/details/18697953

  2. log4j配置文件及nutch中的日志配置 分类: B1_JAVA 2015-02-17 10:58 483人阅读 评论(0) 收藏

    吐槽几句,log4j的坑啊.... (1)CLASSPATH中不能有多个log4j的版本本,否则有有奇形怪状的NoSuchMethod, NoSuchFiled, NoClassDefineFound ...

  3. amazeui学习笔记--css(布局相关3)--辅助类Utility

    amazeui学习笔记--css(布局相关3)--辅助类Utility 一.总结 1.元素清除浮动: 添加 am-cf 这个 class 即可 2.水平滚动: .am-scrollable-horiz ...

  4. Eclipse中开发环境也想把Tomcat 的默认BIO模式改为NIO模式

    1.1 问题 有时候,开发环境我们也想把Tomcat 的默认BIO模式改为NIO模式,该如何改呢? 1.2 方案 通过eclipse里面的server.xml进行修改. 1.3 步骤 首先我们来一起看 ...

  5. VMware Ubuntu安装具体过程

    不是每个程序猿都必须玩过linux,仅仅是博主认为如今的非常多server都是linux系统的,而自己属于那种前端也搞.后台也搞,对框架搭建也感兴趣,可是非常多生产上的框架和工具都是安装在server ...

  6. python课程:python3函数

    摘自廖雪峰的网站:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014316 ...

  7. 3、U-boot的环境变量: bootcmd 和bootargs

    u-bootcmdbootcmd是uboot自动启动时默认执行的一些命令,因此你可以在当前环境中定义各种不同配置,不同环境的参数设置,然后设置bootcmd为你经常使用的那种参数.   现在我的boo ...

  8. 【u252】泽泽在巴西

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 泽泽帮助了英国某街道尽量减少酸雨的伤害,街道办主任非常感激他,就把他领到一扇门前,告诉他这扇门能通往好 ...

  9. Unity插件之NGUI学习(5)—— 创建Label图文混排及文字点击

    创建一个新的Scene,并按 Unity插件之NGUI学习(2)创建UI Root. 准备工作,制作Font.如今Project窗体创建一个Font目录.然后从系统自带字体目录中选择自己须要的字体,我 ...

  10. 博客已迁移至http://blog.csdn.net/lujinhong2/

    http://blog.csdn.net/lujinhong2/ 请继续关注