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 ...
随机推荐
- 01分数规划POJ2976(简单模板题)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7276 Accepted: 2523 De ...
- 代码实现sql数据库的附加(通常在安装的时候)
判断数据库是否已经存在 SqlConnection judgeConn = new SqlConnection("server=.;database=master;uid="+us ...
- start.s 解析(一)
可以参考 : http://blog.csdn.net/bluesummerg/article/details/5940452 (强大的反汇编) http://www.cnblogs.com/yanh ...
- react tab
<!DOCTYPE html> <html> <head> <script src="js/react.js"></scrip ...
- android 三种定位方式
http://www.cnblogs.com/oudi/archive/2012/03/22/2411509.html 最近在看android关于定位的方式,查了很多资料,也做了相关实验,在手机上做了 ...
- OpenGl And 视图
OpenGl And 视图 标签(空格分隔): game 简介 本文主要介绍坐标系的观念, 以及在openGL中的视图及其相关的变换. 大纲 视图.模型.投影变换概念 Opengl中对各种变换的支持 ...
- 博创arm板编译内核makefile不兼容问题解决
导致这种结果的是:Make工具对低版本内核的Makefile一些旧的规则兼容不好,我们只需修改对应的Makefile. 改1:: 原始的: 大概在 1503行 / %/: prepa ...
- 解析php mysql 事务处理回滚操作
论坛扣币项目中,用户支付论坛币的时候如果突然断网.电脑死机.停电.等其它自然灾害时,导致本次交易没有成功(即用户的币已经扣掉了,但是服务器数据库中没有消费记录等其它情况),这种情况应该怎么样进行处理呢 ...
- Server编解码 解决Response.Redirect方法传递汉字丢失或乱码
- SSAS维度上有多个表的注意事项
在Sql Server Analysis Service中维度上有多张表(大于一张表)时,一定要注意将第二张表开始用到维度属性中的KeyColumns下的NullProcessing要设置为Unkno ...