winform抓取淘宝宝贝详细页的上下架时间等信息
2014-05-20 23:08
190 查看
在界面上拖拉几个界面,如下图所示:
后台编码:
运行结果如下:
后台编码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using HtmlAgilityPack; using System.Text.RegularExpressions; public partial class Form9 : Form { private List<Product> proList = new List<Product>(); public Form9() { InitializeComponent(); this.dgResult.Columns["StartTime"].DefaultCellStyle.Format = "yyyy-MM-dd hh:mm:ss"; this.dgResult.Columns["EndTime"].DefaultCellStyle.Format = "yyyy-MM-dd hh:mm:ss"; } /// <summary> /// 将Unix时间戳转换为DateTime类型时间(Unix时间戳指是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数) /// </summary> /// <param name="d">double 型数字</param> /// <returns>DateTime</returns> public System.DateTime UnixToDateTime(long d) { System.DateTime time = System.DateTime.MinValue; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); time = startTime.AddSeconds(d); return time; } /// <summary> /// 提取字符串里面的Unix时间戳 /// </summary> /// <param name="input"></param> /// <param name="pattern"></param> /// <returns></returns> private long GetUnixTick(string input, string pattern) { long result = 0; Regex rx = new Regex(pattern, RegexOptions.IgnoreCase); MatchCollection mc = rx.Matches(input); string value = mc[0].Value; Int64.TryParse(value, out result); if (result > 0)//1401119998000需要去除三个0 result = result / 1000; return result; } private void btnQuery_Click(object sender, EventArgs e) { string input = txtInput.Text.Trim(); string[] arrUrl = Regex.Split(input, "\\s+"); foreach (string url in arrUrl) { string html = Utils.GetHtmlSource(url, Encoding.GetEncoding("GBK")); if (string.IsNullOrEmpty(html)) continue; HtmlNode rootNode = null; HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument(); document.LoadHtml(html); rootNode = document.DocumentNode; string unixStr = GetNodeAttr(rootNode, "//button[@id='J_listBuyerOnView']"); if (string.IsNullOrEmpty(unixStr)) continue; //上架时间 long unixTickStart = GetUnixTick(unixStr, "(?<=starts=)(.*?)(?=&item_id)"); DateTime dtStart = UnixToDateTime(unixTickStart); //下架时间 long unixTickEnd = GetUnixTick(unixStr, "(?<=ends=)(.*?)(?=&starts)"); DateTime dtEnd = UnixToDateTime(unixTickEnd); //标题 string title = GetNodeText(rootNode, "//title"); //价格 decimal price = 0; string priceTmp = GetNodeText(rootNode, "//em[@class='tb-rmb-num']"); decimal.TryParse(priceTmp, out price); proList.Add(new Product() { Url = url, Title = title, Price = price, StartTime = dtStart, EndTime = dtEnd }); } if (proList.Count == 0) { MessageBox.Show("没有找到符合条件的数据,输入网址是否正确?"); return; } var bindingList = new BindingList<Product>(proList); var source = new BindingSource(bindingList, null); dgResult.DataSource = source; } private string GetNodeAttr(HtmlNode rootNode, string path) { HtmlNode temp = rootNode.SelectSingleNode(path); if (temp != null) return temp.Attributes["data-api"].Value; return ""; } private string GetNodeText(HtmlNode rootNode, string path) { HtmlNode temp = rootNode.SelectSingleNode(path); if (temp != null) return temp.InnerText; return ""; } private void btnClear_Click(object sender, EventArgs e) { proList.Clear(); var bindingList = new BindingList<Product>(proList); var source = new BindingSource(bindingList, null); dgResult.DataSource = source; } } public class Product { public string Url { get; set; } public string Title { get; set; } public decimal Price { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } }
运行结果如下:
相关文章推荐
- 200行的Node爬虫花了半天的时间把网易云上的30万首歌曲信息都抓取回来
- 【python爬虫】通过python多线程的抓取新浪新闻的标题时间评论信息
- 【淘宝SEO】淘宝宝贝下架时间分布图
- 200行的Node爬虫花了半天的时间把网易云上的30万首歌曲信息都抓取回来
- WinForm 点击按钮把用户输入的信息导入到Word并以系统时间命名 然后打开这个Word所在文件夹
- Google:收录世上所有信息需要300年时间(zz)
- C#实现web信息自动抓取
- [导入]用C#把winform中的databinding信息取出,并附给datarow
- C#实现web信息自动抓取
- 实例讲解ASP实现抓取网上房产信息
- 用gdi+获取图像的附加信息(metadata)--如jpg照片的标题,相机,曝光时间等
- C#实现web信息自动抓取
- 实例讲解asp抓取网上房产的信息
- C#实现web信息自动抓取
- C#实现web信息自动抓取
- 最近太忙,实在没有时间认真写blog,因此总转些文章来,不过终归是些有益的信息,不是垃圾,也聊以自宽了。
- 在Winform中为TreeView控件增加鼠标旋停时提示节点信息
- JS去空格、IFrame提示信息做后台操作、Cookie存值获取累加,调整过期时间。
- [新]可精确到1毫秒的用以在记录文件中写入自定义的调试信息(主要是时间)的组件
- winform 配置信息的读写