selenium firefox 提取qq空间相册链接
环境:
selenium-java 3.9.1
firefox 57.0
geckodriver 0.19.1
1.大概的思路就是模拟用户点击行为,关于滚动条的问题,我是模拟下拉箭头,否则只能每个相册只能爬到30个链接
2.多开标签页的原因是因为爬取多个相册时,当你爬完第一个相册无论采取什么方式总会导致当前原来的相册列表刷新,从而导致selenium的元素附着失败的异常,所以我的思路是一个相册一个标签页,全部爬取完成后再统一关闭,最开始打开的页面并没有直接用于爬取第一个相册,如果你额外新打开了标签页注意修改for循环中句柄的index
3.使用selenium提取链接效率低下,因为总是要让程序等待页面加载,切换frame,js打开新标签页,句柄切换等页面跳转行为非常耗费时间,链接将按相册名进行保存
4.代码仅供测试,写的并不健壮.严格的讲,只要定位元素就应当try catch,因为drver如果无法find元素就会抛出unlocate异常,没法再去判断元素是否为null了
5.使用前请更改用户名用户密码,如果登录失败,请重新执行,默认登录后等待5s会重新登录
1 package selenium.ff; 2 3 import java.io.File; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 import java.util.ArrayList; 7 import java.util.List; 8 import java.util.concurrent.TimeUnit; 9 import org.apache.commons.io.IOUtils; 10 import org.openqa.selenium.By; 11 import org.openqa.selenium.JavascriptExecutor; 12 import org.openqa.selenium.Keys; 13 import org.openqa.selenium.WebDriver; 14 import org.openqa.selenium.WebElement; 15 import org.openqa.selenium.firefox.FirefoxDriver; 16 import org.openqa.selenium.firefox.FirefoxOptions; 17 import org.openqa.selenium.interactions.Actions; 18 19 /** 20 * 模拟登录qq空间并保存相册的图片链接 21 * @author tele 22 * 23 */ 24 public class QZImage { 25 static final int pageSize = 98; 26 public static void main(String[] args) throws Exception { 27 System.setProperty("webdriver.gecko.driver", "D:/browserdriver/geckodriver.exe"); 28 29 FirefoxOptions options = new FirefoxOptions(); 30 options.setBinary("F:/ff/firefox.exe"); 31 32 WebDriver driver = new FirefoxDriver(options); 33 driver.manage().window().maximize(); 34 // 超时 35 try { 36 driver.manage().timeouts().pageLoadTimeout(3, TimeUnit.SECONDS); 37 driver.manage().timeouts().setScriptTimeout(3, TimeUnit.SECONDS); 38 driver.get("https://i.qq.com/"); 39 } catch (Exception e) { 40 System.out.println("所需元素已出现,停止加载页面"); 41 } finally { 42 // 切换到登录login 43 driver.switchTo().frame("login_frame"); 44 45 WebElement switcher_plogin = driver.findElement(By.id("switcher_plogin")); 46 System.out.println(switcher_plogin.getText()); 47 if (switcher_plogin.isDisplayed()) { 48 switcher_plogin.click(); 49 } 50 // 用户名 51 driver.findElement(By.id("u")).clear(); 52 driver.findElement(By.id("u")).sendKeys("******"); 53 54 // 密码 55 driver.findElement(By.id("p")).clear(); 56 driver.findElement(By.id("p")).sendKeys("******"); 57 58 // 登录 59 try { 60 driver.findElement(By.id("login_button")).click(); 61 Thread.sleep(3000); 62 } catch (Exception e) { 63 e.printStackTrace(); 64 } finally { 65 if ("https://i.qq.com/".equals(driver.getCurrentUrl())) { 66 System.out.println("登录失败!5秒后再次尝试登录"); 67 Thread.sleep(5000); 68 driver.findElement(By.id("login_button")).click(); 69 } 70 } 71 72 // 退出frame 73 driver.switchTo().defaultContent(); 74 75 System.out.println(driver.getCurrentUrl()); 76 77 // 点击相册 78 driver.findElement(By.cssSelector("#menuContainer ul.head-nav-menu>li.menu_item_4>a")).click(); 79 80 Thread.sleep(1000); 81 82 // 切换到frame 83 driver.switchTo().frame(driver.findElement(By.className("app_canvas_frame"))); 84 85 JavascriptExecutor jsExecutor = (JavascriptExecutor) driver; 86 87 // 获得相册列表 88 List<WebElement> photoList = driver.findElements(By.xpath("//ul[@class='js-album-list-ul']/li")); 89 if (photoList == null || photoList.size() == 0) { 90 throw new RuntimeException("定位相册列表元素失败!"); 91 } 92 93 // 构造不同相册的xpath路径 94 List<String> xpathList = new ArrayList<String>(); 95 for (int i = 0; i < photoList.size(); i++) { 96 xpathList.add("//ul[@class='js-album-list-ul']/li[" + (i + 1) + "]"); 97 } 98 99 // 窗口句柄 100 List<String> allHandles = new ArrayList<String>(driver.getWindowHandles()); 101 102 // 遍历xpath 103 String newUrl = driver.getCurrentUrl(); 104 for (int i = 0; i < xpathList.size(); i++) { 105 // 打开新标签页 106 String js = "window.open('" + newUrl + "');"; 107 jsExecutor.executeScript(js); 108 allHandles = new ArrayList<String>(driver.getWindowHandles()); 109 110 Thread.sleep(2000); 111 String xpath = xpathList.get(i); 112 113 // 句柄切换需要时间 114 driver.switchTo().window(allHandles.get(i + 1)); 115 Thread.sleep(2000); 116 saveImageUrl(driver, xpath); 117 } 118 119 System.out.println("所有相册图片链接提取完毕,退出浏览器"); 120 driver.quit(); 121 122 } 123 124 } 125 126 /** 127 * 提取图片url 128 * 129 * @param driver 130 * @param xpath 131 * @throws InterruptedException 132 * @throws IOException 133 */ 134 public static void saveImageUrl(WebDriver driver, String xpath) throws InterruptedException, IOException { 135 136 // 点击相册 137 driver.findElement(By.cssSelector("#menuContainer ul.head-nav-menu>li.menu_item_4>a")).click(); 138 139 Thread.sleep(3000); 140 141 // 切换到图片的frame 142 driver.switchTo().frame(driver.findElement(By.className("app_canvas_frame"))); 143 144 // 获得相册名称 145 String photo_name = driver.findElement(By.xpath(xpath + "//a[@class='c-tx2 js-album-desc-a']")).getText(); 146 147 //// 文件夹检测 148 File imageUrl = new File("f:/qz/" + photo_name + ".txt"); 149 if (!imageUrl.getParentFile().exists()) { 150 imageUrl.mkdirs(); 151 } else { 152 imageUrl.delete(); 153 } 154 155 // 获得图片总数,每页最多98张图片 156 WebElement span = driver.findElement(By.xpath(xpath + "/div[1]/div[1]/a" + "/span")); 157 String text = span.getText(); 158 int count = Integer.parseInt(text); 159 160 // 进入列表 161 driver.findElement(By.xpath(xpath + "/div[1]/div[1]/a")).click(); 162 Thread.sleep(3000); 163 164 // 计算页数 165 int totalPage = (int) Math.ceil((double) count / (double) pageSize); 166 System.out.println(photo_name + "图片总数为----" + count + "张,共计---" + totalPage + "页"); 167 168 FileWriter fileWriter = new FileWriter(imageUrl, true); 169 170 for (int i = 0; i < totalPage; i++) { 171 172 // 模拟按键加载图片 173 Actions actions = new Actions(driver); 174 for (int j = 0; j < 50; j++) { 175 if (j % 5 == 0) { 176 Thread.sleep(1000); 177 } 178 actions.sendKeys(Keys.ARROW_DOWN).perform(); 179 } 180 181 // 提取本页的image链接 182 List<WebElement> list = driver.findElements(By.xpath("//img[@class='j-pl-photoitem-img']")); 183 if (list == null || list.size() == 0) { 184 // 相册无权限访问或定位失败 185 throw new RuntimeException("无法提取图片链接!"); 186 } 187 for (WebElement element : list) { 188 String src = element.getAttribute("src") + "\n"; 189 IOUtils.write(src, fileWriter); 190 } 191 System.out.println("第" + (i + 1) + "页图片链接提取完毕"); 192 193 Thread.sleep(1000); 194 195 // 跳转到下一页 196 if ((i + 2) <= totalPage) { 197 driver.findElement(By.xpath("//a[@id='pager_num_1_" + (i + 2) + "']")).click(); 198 } 199 } 200 201 fileWriter.close(); 202 } 203 }
转载于:https://www.cnblogs.com/tele-share/p/9595265.html
selenium firefox 提取qq空间相册链接相关推荐
- python自动下载qq文件夹_GitHub - 1061700625/QQZone_AutoDownload_Album: Python+selenium 自动下载QQ空间相册...
QQZone_AutoDownload_Album Python+selenium 自动下载QQ空间相册 . selenium_firefox.zip 需要解压后放在同路径下 . 貌似腾讯的登陆加密做 ...
- php爬取qq好友,使用php批量抓取QQ空间相册链接
前言 小杰之前发布的文章<为什么QQ空间与我们渐行渐远?> 里面就提到小杰会抽空备份QQ空间的照片,但是在网上找了很久也没有找到一个有效的工具 作为一个Phper,淦就完事了,所以顶着寒冷 ...
- Python_小林的爬取QQ空间相册图片链接程序
前言 昨天看见某人的空间有上传了XXXX个头像,然后我就想着下载回来[所以本质上这是一个头像下载程序],但是一个个另存为太浪费时间了,上网搜索有没有现成的工具,居然要注册码,还卖45一套.你们的良心也 ...
- python访问陌生人qq空间_使用Python+Selenium模拟登录QQ空间
使用Python+Selenium模拟登录QQ空间 爬QQ空间之类的页面时大多需要进行登录,研究QQ登录规则的话,得分析大量Javascript的加密解密,这绝对能掉好几斤头发.而现在有了seleni ...
- Python网络爬虫5 - 爬取QQ空间相册
自毕业后,就再也没有用过QQ,QQ空间里记录的是些并不精彩的青葱岁月,但好歹也是份回忆,近日想着学以致用,用Python把QQ空间相册的所有照片爬取下来,以作备份. 分析QQ空间 登录QQ空间 爬取第 ...
- 如何备份你的 QQ 空间相册
领红包方法见文末 周末无聊的时候打开了下久违的QQ空间,那么多年的记录都还在.曾经建的杰伦相册. 想到之前网易相册都关闭了,虽然腾讯家大业大,但万一呢,于是想着把QQ空间的图片都备份下来.于是有了今天 ...
- QQ空间将不再支持免费备份原图?附QQ空间相册导出工具合集
感谢您抽出 .. 阅读本文 小伙伴们注意:公众号的推送机制不再按照时间前后推送了,微信公众号信息流乱序.君哥建议大家把科技毒瘤君公众号置顶(设为星标⭐),以便第一时间看到推送,非常感谢~,方法如下图: ...
- php实现qq相册功能,使用javascript如何实现QQ空间相册展示
本文给大家分享基于javascript制作的qq空间相册展示效果,涉及到html\css布局思维,浮动定位详解,具体实现代码大家参考下本文 知识点:html/css布局思维,浮动/定位详解,大企业标准 ...
- QQ空间相册展示特效
<!doctype html> <html lang="en"> <head> <title>QQ空间相册展示特效<title ...
最新文章
- Nmap安装和扫描(二:Nmap基本操作)
- matlab中clc,close,close all,clear,clear all作用区别
- 进程知识点,只需这一篇
- 自定义Android时钟(支持秒针)
- python成长之路11
- 跨越行业绊脚石,阿里云函数计算发布 7 大技术突破
- 贪心算法之——独木舟上的旅行(nyoj71)
- 注册中心—组件—Consul
- linux中544进程,Linux基础--进程管理及其基本命令
- 菜鸟学Java(六)——简单验证码生成(Java版)
- linux 安装python3.8的几种方法
- Eclipse设置字体大小
- android获得手机目录,关于android手机文件目录的收集
- 凤凰项目:一个IT运维的传奇故事
- NginxWebUI - 图形化的 Nginx 配置管理工具
- webssh的安装与使用
- 珍珠项链(洛谷-P2768)(Dp矩阵加速)
- 【Visual C++】游戏开发笔记三十七 浅墨DirectX提高班之五 顶点缓存的红颜知己 索引缓存的故事
- 监控视频中的主码流和子码流是什么意思?
- HTML5期末大作业:汉堡美食网站设计——餐饮美食-汉堡美食(6页) HTML+CSS+JavaScript 汉堡美食 咖啡主题HTM5网页设计作业成品