我用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. 利用并查集求最大生成树和最小生成树(nlogn)

    hdu1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  2. SQL 存储和触发器

    存储过程:就像函数一样的会保存在数据库中-->可编程性 --> 存储过程 创建存储过程:create proc JiaFa--需要的参数@a int,@b intas --存储过程的内容 ...

  3. prezi破解教程

    http://www.joenchen.com/archives/998 http://www.joenchen.com/archives/945 Prezi Desktop 4.7.5免注册无时间限 ...

  4. 如何在OneNote2013中粘贴高亮的代码

    有的时候想在OneNote粘贴代码,但是直接复制粘贴进去的代码没有高亮,下面有一个办法让自己的代码在OneNote里面更加完整美观. 工具/原料 Notepad++ word2013 OneNote2 ...

  5. Android 屏幕滑动事件

    http://blog.csdn.net/iamfafa/article/details/6316062 Android里有两个类 android.view.GestureDetector andro ...

  6. ajax中网页传输(二)JSON——下拉列表显示练习

    以json返回数据类型显示“民族下拉列表” 第一:body页面显示部分 <title>JSON下拉显示Nation表中的数据</title> <script src=&q ...

  7. SQL Server数据库的三种恢复模式:简单恢复模式、完整恢复模式和大容量日志恢复模式(转载)

    SQL Server数据库有三种恢复模式:简单恢复模式.完整恢复模式和大容量日志恢复模式: 1.Simple 简单恢复模式, Simple模式的旧称叫”Checkpoint with truncate ...

  8. 解决Android SDK Manager更新下载速度慢

    1.在Host文件加上 203.208.46.146 www.google.com 74.125.113.121 developer.android.com 203.208.46.146 dl.goo ...

  9. simpleBLEPeripheral.c 文件分析

    这个配置或者说任务, 让这个蓝牙设备成为了一个简单的BLE外设. 这里定义了外设的广播数据, 以及最重要, char被改变之后的回调, 引出后来的coreHandler里面的, ack 以及写e2pr ...

  10. 使用jprofiler8远程监控weblogic的配置方法

    jprofiler8的注册码:L-Larry_Lau@163.com#36573-fdkscp15axjj6#25257 未完待续