C#中通过Selenium IWebDriver实现人人网相册备份工具

简介: 我用Selenium写了一个人人网相册备份工具,亲测通过。 需要输入你的用户名、密码、相册地址。 代码如下: using System; using System.Collections.Generic; using System.

我用Selenium写了一个人人网相册备份工具,亲测通过。

需要输入你的用户名、密码、相册地址。

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
using System.Threading;
using System.IO;
using System.Net;

namespace RenrenBackup
{
    public class Program
    {
        static void Main(string[] args)
        {
            //Configuration.
            Program pp = new Program();
            string url = "http://www.renren.com";
            string userName = "";
string pwd = ""; string albumUrl = ""; //Start backup. IWebDriver iw = new InternetExplorerDriver(); iw = pp.Login(url, pwd, userName, iw); iw.Navigate().GoToUrl(albumUrl); pp.WaitUntilPageLoadedID(iw, "c-b-describe"); List<string> photoUrls = new List<string>(); var photos = iw.FindElements(By.ClassName("photo-box")); var count = iw.FindElement(By.Id("album-count")).Text; while (photos.Count() != Int32.Parse(count)) { try { String setScroll = "document.documentElement.scrollTop=" + 9000; ((IJavaScriptExecutor)iw).ExecuteScript(setScroll); Thread.Sleep(3000); photos = iw.FindElements(By.ClassName("photo-box")); } catch (Exception ex) { WriteLog(ex); } } string value; for (int i = 0; i < photos.Count(); i++) { value = iw.FindElements(By.XPath("//a/img[@class='p-b-item']"))[i].GetAttribute("data-viewer"); value = value.Split(',')[4].Split('"')[3].Trim(); photoUrls.Add(value); } pp.WriteToFile(photoUrls); string filePath; string folderPath = @Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\" + iw.FindElement(By.Id("album-name")).Text; if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } for (int i = 0; i < photoUrls.Count(); i++) { filePath = folderPath + @"\" + i + "pic.jpg"; WebClient myWebClient = new WebClient(); try { myWebClient.DownloadFile(photoUrls[i], filePath); } catch (Exception ex) { WriteLog(ex); } } } public void WriteToFile(List<string> photoUrls) { string file = @Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\PhotoUrls.txt"; if (File.Exists(file)) { using (StreamWriter sw = new StreamWriter(file, true)) { foreach (string photoUrl in photoUrls) { sw.Write(photoUrl + "\r\n"); sw.Flush(); } } } else { File.Create(file).Close(); using (StreamWriter sw = new StreamWriter(file, true)) { foreach (string photoUrl in photoUrls) { sw.Write(photoUrl + "\r\n"); sw.Flush(); } } } } public void WriteToFile(string v) { string file = @Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\PhotoUrls.txt"; if (File.Exists(file)) { using (StreamWriter sw = new StreamWriter(file, true)) { sw.Write(v + "\r\n"); sw.Flush(); } } else { File.Create(file).Close(); using (FileStream fs = new FileStream(file, FileMode.Open)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(v + "\r\n"); sw.Flush(); } } } } public IWebDriver Login(string url, string pwd, string userName, IWebDriver iw) { iw.Navigate().GoToUrl(url); WaitUntilPageLoadedID(iw, "email"); iw.FindElement(By.Id("email")).SendKeys(userName); iw.FindElement(By.Id("password")).SendKeys(pwd); iw.FindElement(By.Id("login")).Click(); WaitUntilPageLoadedXPath(iw, "//span[text()='我的相册']"); return iw; } public void WaitUntilPageLoadedID(IWebDriver iw, string id) { try { iw.FindElement(By.Id(id)); } catch (Exception ex) { Console.WriteLine(ex); Thread.Sleep(2000); WaitUntilPageLoadedID(iw, id); } } public void WaitUntilPageLoadedXPath(IWebDriver iw, string v) { try { iw.FindElement(By.XPath(v)); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Thread.Sleep(1000); WaitUntilPageLoadedXPath(iw, v); } } private static void WriteLog(Exception ex) { string logUrl = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\DownloadLog.txt"; if (File.Exists(@logUrl)) { using (FileStream fs = new FileStream(logUrl, FileMode.Append)) { using (StreamWriter sw = new StreamWriter(fs, Encoding.Default)) { try { sw.Write(ex); } catch (Exception ex1) { WriteLog(ex1); } finally { sw.Close(); fs.Close(); } } } } else { using (FileStream fs = new FileStream(logUrl, FileMode.CreateNew)) { using (StreamWriter sw = new StreamWriter(fs, Encoding.Default)) { try { sw.Write(ex); } catch (Exception ex1) { WriteLog(ex1); } finally { sw.Close(); fs.Close(); } } } } } } }

引用如下:

注意设置IEDriverServer.exe的属性为“Copy aLways”。

运行后会在桌面生成一个和你人人网相册同名的文件夹,下面保存的是你的相册相片。桌面还会有下载的图片的详细url列表以及如果有异常会产生的log文件。

OK,回家。

相关文章
|
1月前
|
Web App开发 前端开发 测试技术
探索自动化测试工具:Selenium的威力与应用
探索自动化测试工具:Selenium的威力与应用
探索自动化测试工具:Selenium的威力与应用
|
2月前
|
自然语言处理 C# Windows
C#开源免费的Windows右键菜单管理工具
C#开源免费的Windows右键菜单管理工具
|
20天前
|
Web App开发 Java 测试技术
深入理解与应用软件自动化测试工具Selenium
随着软件开发的快速发展,软件测试在保证产品质量方面发挥着越来越重要的作用。其中,自动化测试以其效率高、成本低的特点受到了广大开发者的欢迎。本文主要介绍了自动化测试工具Selenium的基本概念、原理以及在实际开发中的应用,旨在帮助读者更好地理解和使用Selenium进行高效的自动化测试。
22 4
|
29天前
|
Web App开发 前端开发 JavaScript
Python Selenium是一个强大的自动化测试工具
Python Selenium是一个强大的自动化测试工具
|
5月前
|
缓存 开发框架 监控
一个C#开发的开源的快速启动工具
一个C#开发的开源的快速启动工具
43 0
|
2月前
|
Web App开发 测试技术 数据安全/隐私保护
Web自动化测试工具Selenium
Web自动化测试工具Selenium
|
2月前
|
监控 测试技术 API
自动化测试工具与电脑桌面监控软件的集成:Selenium与Python的无缝整合
在当今数字化时代,软件质量保证是每个软件开发团队都必须面对的重要挑战之一。自动化测试工具和电脑桌面监控软件的结合,为开发团队提供了一种有效的方式来确保软件的稳定性和性能。本文将介绍如何利用Python编程语言中的Selenium库,与桌面监控软件进行无缝整合,以实现对应用程序的自动化测试和桌面监控。
184 5
|
3月前
|
Web App开发 数据采集 JavaScript
【Python爬虫】<万物可爬>Selenium+自动化测试工具 获取数据
【1月更文挑战第22天】【Python爬虫】<万物可爬>Selenium+自动化测试工具 获取数据
|
3月前
|
存储 安全 算法
C# 泛型:类型参数化的强大工具
【1月更文挑战第7天】本文将深入探讨C#语言中的泛型编程,包括泛型的定义、用途、优势以及实际应用。通过类型参数化,泛型允许开发者编写更加灵活且可重用的代码,同时提高程序的类型安全性和性能。本文将通过示例代码和详细解释,帮助读者更好地理解泛型在C#中的重要性和实用性。
|
4月前
|
JSON C# 图形学
【Unity 3D】利用C#、Unity和Socket实现简单的在线聊天室工具(附源码 简单易懂)
【Unity 3D】利用C#、Unity和Socket实现简单的在线聊天室工具(附源码 简单易懂)
48 0