C#中通过Selenium IWebDriver实现人人网相册备份工具
我用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实现人人网相册备份工具的更多相关文章
- eclipse中运行Selenium遇到的问题
1. java.lang.NoClassDefFoundError: 解决方法:eclipse的java工程中导入selenium-java-2.44.0\selenium-2.44.0\libs ...
- Java中通过Selenium WebDriver定位iframe中的元素
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 问题:有一些元素,无论是通过id或是xpath等等,怎么都定位不到. 分析:这很可能是因为你要定位 ...
- 在Scrapy中使用selenium
在scrapy中使用selenium 在scrapy中需要获取动态加载的数据的时候,可以在下载中间件中使用selenium 编码步骤: 在爬虫文件中导入webdrvier类 在爬虫文件的爬虫类的构造方 ...
- Jenkins中配置selenium测试
Jenkins中配置selenium测试 2015/03/23 第一步在jenkins中配置selenium服务器 第二步工程配置: 第三步:执行构建: 第四步,查看报告:
- 【转】JMeter中使用Selenium进行测试
JMeter是使用非常广泛的性能测试工具,而Selenium是ThroughtWorks 公司一个强大的开源Web 功能测试工具.Jmeter和Selenium结合使用,就可以实现对网站页面的自动化性 ...
- 在linux和windows中使用selenium
在linux和windows中使用selenium 一. selenium(浏览的人你们多大呀?是AI?) selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法 ...
- Django学习系列2:django环境中安装selenium并查看selenium版本号
在Django环境中安装selenium (django) root@ranxf-TEST:/studydisk/Python_web_TDD/superlists# conda install se ...
- 火狐中添加selenium IDE
在火狐中添加selenium IDE 1.下载selenium IDE,此处下载的是selenium-ide-2.5.0.xpi 2.在火狐中,打开菜单-->附加组件-->用户附加组件的工 ...
- scrapy中间件中使用selenium切换ip
scrapy抓取一些需要js加载页面时一般要么是通过接口直接获取数据,要么是js加载,但是我通过selenium也可以获取动态页面 但是有个问题,容易给反爬,因为在scrapy中间件mid中使用sel ...
随机推荐
- JAVA-面向对象-特性
1.封装 1.定义方式 1修饰符class类名 2类名首字母大写 2.类的成员 1属性 成员变量 可以设置默认值 第一个单词首字母小写,后面首字母大写 一般把属性设置成private 提供属性对应的g ...
- 转:Curl详解
用途说明 curl命令是一个功能强大的网络工具,它能够通过http.ftp等方式下载文件,也能够上传文件.其实curl远不止前面所说的那些功能,大家可以通过man curl阅读手册页获取更多的信息.类 ...
- Java基础(40):Java中的集合介绍---Collection与Map
集合类说明及区别Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap└WeakHashMap ...
- C++之路进阶——bzoj2199(奶牛议会)
F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser gryz2016 Logout 捐赠本站 Notice:由于本OJ ...
- paper 10:支持向量机系列七:Kernel II —— 核方法的一些理论补充,关于 Reproducing Kernel Hilbert Space 和 Representer Theorem 的简介。
在之前我们介绍了如何用 Kernel 方法来将线性 SVM 进行推广以使其能够处理非线性的情况,那里用到的方法就是通过一个非线性映射 ϕ(⋅) 将原始数据进行映射,使得原来的非线性问题在映射之后的空间 ...
- 搞不定linux下的无线网卡驱动的权宜之计
毕竟windows用了这么些年了,对windows下的一些东西也比较熟悉,还有就是windows的软件方式比较傻瓜. 在linux下搞不定无线网卡啊,幸亏有甲骨文的virtualbox,咱虚拟一个xp ...
- 【linux】终端直接执行py文件,不需要python命令
先将终端所在路径切换到python脚本文件的目录下然后给脚本文件运行权限,一般755就OK,如果完全是自己的私人电脑,也不做服务器什么的,给777的权限问题也不大(具体权限含义参考chmod指令的介绍 ...
- 视频处理控件TVideoGrabber中如何混合多个视频源(1)
其实一个或是几个作为普通的视频源使用的TVideoGrabber组件,可以进行混合来作为一个TVideoGrabber组件使用,这些普通的组件可以是视频捕捉设备或是视频剪辑等.同时这个混合的组件独立于 ...
- 鸟哥的linux私房菜学习记录之开机流程、模块管理与Loader
- HttpWebRequest:百度定位当前位置解析
HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create("http://api.map.baidu.com ...