|
为帮助公司客户更好的进行抓取和发布,完成特定任务,我们发布了火车浏览器开发版SDK。用户可以通过各种程序,使用命令行的方式调用火车浏览器的任务运行器,就可以得到浏览器运行完成后的结果。
SDK下载地地址:http://www.locoyposter.com/LpSDK.rar,下载后注意将最新版的火车浏览器解压到项目的Debug目录下,即可以测试使用。
该SDK中包含c#和Java版两种语言开发示例,文档中也包含使用说明文档。具体参数请看代码说明和参考C#.
开发者下载后,请打开默认的C#项目,查看各种参数,如有疑问,请回帖或群内提问。下边是C#的调用的主要方法。请使用visual studio 2008或更高版本打开。
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Diagnostics;
- namespace LpSDK
- {
- class Program
- {
- static void Main(string[] args)
- {
- TaskConfig tc = new TaskConfig();
- tc.TaskFile = AppDomain.CurrentDomain.BaseDirectory + "Projects\\testcmd.lp"; ;
- tc.SKey = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(DateTime.Now.ToString("yyyyMMdd"), "MD5").ToLower().Substring(8, 16);
- tc.LogFilePath = AppDomain.CurrentDomain.BaseDirectory + "test.txt";
- tc.ResultFile = AppDomain.CurrentDomain.BaseDirectory + "result.xml";
- tc.TaskName = "演示火车浏览器SDK的使用";
- string app = AppDomain.CurrentDomain.BaseDirectory + "TasksRunner.exe";
- tc.Varlist.Add("关键词", "改革");
- if (System.IO.File.Exists(tc.LogFilePath)) System.IO.File.Delete(tc.LogFilePath);
- if (System.IO.File.Exists(tc.ResultFile)) System.IO.File.Delete(tc.ResultFile);
- string arg = tc.ToString();
- ProcessStartInfo processStartInfo = new ProcessStartInfo();
- processStartInfo.FileName = app;
- processStartInfo.Arguments = arg;
- Process process = Process.Start(processStartInfo);
- while (process != null && !process.HasExited)
- {
- System.Threading.Thread.Sleep(200);
- }
- //读取结果或日志进行分析,有些任务是不需要返回结果文件可以为空,通过其它方式判断是否成功。
- if (System.IO.File.Exists(tc.ResultFile))
- {
- System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
- doc.Load(tc.ResultFile);
- string title = doc.DocumentElement.SelectSingleNode("//String/标题").InnerText;
- string url = doc.DocumentElement.SelectSingleNode("//String/采集页网址").InnerText;
- Console.WriteLine(string.Format("标题:{0}\r\n网址:{1}", title, url));
- //<?xml version="1.0" encoding="utf-8"?>
- //<Root>
- // <List name="ls">
- // <value>http://news.qq.com/a/20140819/004539.htm?tu_biz=1.114.1.0</value>
- // <value>http://news.qq.com/a/20140819/002046.htm?tu_biz=1.114.1.0</value>
- // <value>http://news.qq.com/a/20140819/001181.htm?tu_biz=1.114.1.0</value>
- // <value>http://news.qq.com/a/20140818/079323.htm?tu_biz=1.114.1.0</value>
- // <value>http://news.qq.com/a/20140819/014417.htm?tu_biz=1.114.1.0</value>
- // <value>http://news.qq.com/a/20140819/011585.htm?tu_biz=1.114.1.1</value>
- // <value>http://news.qq.com/a/20140819/010341.htm?tu_biz=1.114.1.1</value>
- // <value>http://news.qq.com/a/20140819/014565.htm?tu_biz=1.114.1.1</value>
- // </List>
- // <String>
- // <va>
- // </va>
- // </String>
- //</Root>
- }
- else
- {
- Console.WriteLine("运行失败,没有找到结果文件");
- }
- Console.ReadKey();
- }
- }
- }
复制代码
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Xml;
- namespace LpSDK
- {
- public class TaskConfig
- {
- /// <summary>
- /// 任务运行名称
- /// </summary>
- public string TaskName = "";
- /// <summary>
- /// 任务脚本文件,不得为空
- /// </summary>
- public string TaskFile = "";
- /// <summary>
- /// 开发版SDK的Key
- /// </summary>
- public string SKey = "";
- /// <summary>
- /// 缓存文件目录,不指定系统自动创建
- /// </summary>
- public string CacheFiles = "";
- /// <summary>
- /// 浏览器窗口显示模式,0:默认大小,1:最小化,2:最大化
- /// </summary>
- public int WindowState = 0;
- /// <summary>
- /// 日志文件目录,空为不写日志
- /// </summary>
- public string LogFilePath = "";
- /// <summary>
- /// 返回的所有变量值保存的文件,为空不返回值
- /// </summary>
- public string ResultFile = "";
- /// <summary>
- /// 脚本运行完成后界面还显示多长时间,方便查看
- /// </summary>
- public int ShowTime = 0;
- /// <summary>
- /// 所有变量的键值对
- /// </summary>
- public Dictionary<string, string> Varlist = new Dictionary<string, string>();
- /// <summary>
- /// 将配置文件生成xml并urlencode处理
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- if (!System.IO.File.Exists(TaskFile)) throw new Exception("脚本文件不存在");
- if (string.IsNullOrEmpty(SKey)) throw new Exception("SKey不得为空");
- System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
- System.Xml.XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
- doc.AppendChild(dec);
- System.Xml.XmlElement root = doc.CreateElement("root");
- if (!string.IsNullOrEmpty(TaskName)) root.SetAttribute("TaskName", TaskName);
- root.SetAttribute("TaskFile", TaskFile);
- root.SetAttribute("SKey", SKey);
- if (!string.IsNullOrEmpty(CacheFiles)) root.SetAttribute("CacheFiles", CacheFiles);
- root.SetAttribute("WindowState", WindowState.ToString());
- if (!string.IsNullOrEmpty(LogFilePath)) root.SetAttribute("LogFilePath", LogFilePath);
- if (!string.IsNullOrEmpty(ResultFile)) root.SetAttribute("ResultFile", ResultFile);
- root.SetAttribute("ShowTime", ShowTime.ToString());
- XmlNode varlist = doc.CreateNode(XmlNodeType.Element, "Varlist", "");
- if (this.Varlist.Count > 0)
- {
- foreach (KeyValuePair<string, string> kv in Varlist)
- {
- XmlNode node = doc.CreateNode(XmlNodeType.Element, kv.Key, "");
- node.InnerText = kv.Value;
- varlist.AppendChild(node);
- }
- }
- root.AppendChild(varlist);
- doc.AppendChild(root);
- string xml = doc.OuterXml;
- /*
- <?xml version="1.0" encoding="utf-8"?><root TaskFile="F:\LpSDK\bin\Debug\Projects\testcmd.lp" SKey="081c5f101bed6d44" WindowState="0" LogFilePath="F:\LpSDK\bin\Debug\test.txt" ResultFile="F:\LpSDK\bin\Debug\result.xml" ShowTime="0"><Varlist><关键词>改革</关键词></Varlist></root>
- * */
- return System.Web.HttpUtility.UrlEncode(xml);
- }
- }
- }
复制代码
|
|