我用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=" + ;
((IJavaScriptExecutor)iw).ExecuteScript(setScroll);
Thread.Sleep();
photos = iw.FindElements(By.ClassName("photo-box"));
}
catch (Exception ex)
{
WriteLog(ex);
}
}
string value;
for (int i = ; i < photos.Count(); i++)
{
value = iw.FindElements(By.XPath("//a/img[@class='p-b-item']"))[i].GetAttribute("data-viewer");
value = value.Split(',')[].Split('"')[].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 = ; 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();
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();
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,回家。

C#中通过Selenium IWebDriver实现人人网相册备份工具的更多相关文章

  1. eclipse中运行Selenium遇到的问题

    1.   java.lang.NoClassDefFoundError: 解决方法:eclipse的java工程中导入selenium-java-2.44.0\selenium-2.44.0\libs ...

  2. Java中通过Selenium WebDriver定位iframe中的元素

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 问题:有一些元素,无论是通过id或是xpath等等,怎么都定位不到. 分析:这很可能是因为你要定位 ...

  3. 在Scrapy中使用selenium

    在scrapy中使用selenium 在scrapy中需要获取动态加载的数据的时候,可以在下载中间件中使用selenium 编码步骤: 在爬虫文件中导入webdrvier类 在爬虫文件的爬虫类的构造方 ...

  4. Jenkins中配置selenium测试

    Jenkins中配置selenium测试 2015/03/23 第一步在jenkins中配置selenium服务器 第二步工程配置: 第三步:执行构建: 第四步,查看报告:

  5. 【转】JMeter中使用Selenium进行测试

    JMeter是使用非常广泛的性能测试工具,而Selenium是ThroughtWorks 公司一个强大的开源Web 功能测试工具.Jmeter和Selenium结合使用,就可以实现对网站页面的自动化性 ...

  6. 在linux和windows中使用selenium

     在linux和windows中使用selenium 一. selenium(浏览的人你们多大呀?是AI?) selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法 ...

  7. Django学习系列2:django环境中安装selenium并查看selenium版本号

    在Django环境中安装selenium (django) root@ranxf-TEST:/studydisk/Python_web_TDD/superlists# conda install se ...

  8. 火狐中添加selenium IDE

    在火狐中添加selenium IDE 1.下载selenium IDE,此处下载的是selenium-ide-2.5.0.xpi 2.在火狐中,打开菜单-->附加组件-->用户附加组件的工 ...

  9. scrapy中间件中使用selenium切换ip

    scrapy抓取一些需要js加载页面时一般要么是通过接口直接获取数据,要么是js加载,但是我通过selenium也可以获取动态页面 但是有个问题,容易给反爬,因为在scrapy中间件mid中使用sel ...

随机推荐

  1. Summary: Process & Tread

    refer to http://www.programmerinterview.com/index.php/operating-systems/thread-vs-process/ A process ...

  2. zabbix监控linux文件的一个目录大小

    监控linux文件的一个目录大小 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.       欢迎加入:高级运维工程师之路 598432640 我们知道,Linux自带的一些监控模板 ...

  3. ACM之Java速成(1)

    这里指的java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目. 1. 输入: 格式为:Scanner cin = new Sc ...

  4. android 测试(转)

    个人接触android的时间也不是很长,稍微总结下在做Android测试的过程中,初次接触的同学需要些什么准备,以及需要些什么知识?下面讲到的东西可能很多人会觉得很简单,但我确实碰到过有新同学对这些点 ...

  5. yii中sphinx,Ajax搜索分页

    效果图: 控制器: <?phpnamespace backend\controllers; use Yii;use yii\web\Controller;use yii\data\Paginat ...

  6. Fresco源码解析 - 创建一个ImagePipeline(一)

    在Fresco源码解析 - 初始化过程分析章节中, 我们分析了Fresco的初始化过程,两个initialize方法中都用到了 ImagePipelineFactory类. ImagePipeline ...

  7. VS2012发布网站详细步骤

    1.打开你的VS2012网站项目,右键点击项目>菜单中 重新生成一下网站项目:再次点击右键>发布: 2.弹出网站发布设置面板,点击<新建..>,创建新的发布配置文件: 输入你自 ...

  8. jquery 下拉菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. C# PDFBox 解析PDF文件

    下载 PDFBox-0.7.3.zip PDFBox-0.7.3.dlllucene-demos-2.0.0.dlllucene-core-2.0.0.dllbcmail-jdk14-132.dllb ...

  10. 解决 “invalid resource directory name”, resource “crunch”

        try this: from the menu click Project->Clean... a popup window will appear. select the check ...