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,回家。

相关文章
|
Web App开发 数据采集 C#
解决Firefox代理身份验证弹出窗口问题:C#和Selenium实战指南
本文是一份实战指南,主要介绍了在使用Selenium和C#进行网页抓取时,如何设置代理服务器的身份验证以避免自动化流程中断。文章首先列出了所需的开发环境和工具,然后通过C#代码示例详细展示了如何在Firefox浏览器中设置代理IP、端口、用户名、密码以及UserAgent和Cookies。代码中包含了自动处理代理身份验证弹出窗口的配置,以及如何添加Cookies的方法。最后,文章强调了结合C#和Selenium可以提高网页抓取任务的稳定性和效率。
366 3
解决Firefox代理身份验证弹出窗口问题:C#和Selenium实战指南
|
6月前
|
Java Python
介绍一款更好用的selenium自愈工具ReCheck
前面介绍了GUI自动化自愈工具Healenium,现在介绍另一个自愈工具ReCheck
310 7
|
前端开发 测试技术 Python
【Selenium全攻略】掌握这一工具,实现自动化测试的所有梦想
本文分享了使用Selenium进行UI自动化测试的全过程,包括开发环境部署、代码实现、思路分析和难点解析。作者通过一个实际案例,讲述了如何利用Selenium处理前端生成报告失败的问题,以及在UI自动化中定位元素和处理元素不唯一的情况。同时,文章强调了解决问题思路的重要性,鼓励读者开拓思维,寻找不同的解决方案。
550 4
【Selenium全攻略】掌握这一工具,实现自动化测试的所有梦想
|
数据采集 数据可视化 测试技术
C#生成Selenium测试报告:实用方法与技巧
在C#中使用Selenium进行自动化测试时,结合代理IP和ExtentReports能增强测试安全性和报告质量。安装必备工具如Selenium WebDriver、NUnit和ExtentReports。在测试设置中,配置代理(如亿牛云爬虫代理)以隐藏IP,通过ChromeOptions定制UserAgent,并添加Cookie。测试代码示例展示了如何打开网页、执行搜索并生成详细的测试报告。使用ExtentReports可创建可视化测试结果,便于团队分析。
295 5
C#生成Selenium测试报告:实用方法与技巧
|
IDE C# 开发工具
一个开源轻量级的C#代码格式化工具(支持VS和VS Code)
一个开源轻量级的C#代码格式化工具(支持VS和VS Code)
539 6
|
Web App开发 IDE 测试技术
Selenium:强大的 Web 自动化测试工具
Selenium 是一款强大的 Web 自动化测试工具,包括 Selenium IDE、WebDriver 和 Grid 三大组件,支持多种编程语言和跨平台操作。它能有效提高测试效率,解决跨浏览器兼容性问题,进行性能测试和数据驱动测试,尽管存在学习曲线较陡、不稳定等缺点,但其优势明显,是自动化测试领域的首选工具。
1012 17
Selenium:强大的 Web 自动化测试工具
|
Web App开发 测试技术 API
自动化测试工具Selenium的深度解析
【5月更文挑战第27天】本文旨在深入剖析自动化测试工具Selenium,探讨其架构、原理及应用。通过对其核心组件、运行机制及在实际项目中的应用案例进行详细解读,以期为软件测试人员提供全面、深入的理解与实践指导。
|
程序员 C# 数据库
C# 比较对象新思路,利用反射技术打造更灵活的比较工具
中途接手的项目,碰到需要在更新对象信息时比较并记录差异的需求,最变态的还有附加要求,怎么办?有没有既能满足需求又能对项目影响最小的方法呢?分享这个我封装的方法,一个利用反射技术打造的更灵活的比较工具
245 5
|
Web App开发 Java
使用java操作浏览器的工具selenium-java和webdriver下载地址
【10月更文挑战第12天】Selenium-java依赖包用于自动化Web测试,版本为3.141.59。ChromeDriver和EdgeDriver分别用于控制Chrome和Edge浏览器,需确保版本与浏览器匹配。示例代码展示了如何使用Selenium-java模拟登录CSDN,包括设置驱动路径、添加Cookies和获取页面源码。
1034 6
|
XML 存储 安全
C#开发的程序如何良好的防止反编译被破解?ConfuserEx .NET混淆工具使用介绍
C#开发的程序如何良好的防止反编译被破解?ConfuserEx .NET混淆工具使用介绍
1565 0