using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Test.Solution.DataGridView.Caiji
{
public class Caiji
{
private List<Cowry> m_Data;
private string Link = "";
private WebBrowser m_Web;
private int m_Paged = 1;
private delegate void CollBack(Cowry model);
private Caiji()
{
m_Data = new List<Cowry>();
m_Web = new WebBrowser();
}
private static Caiji m_Oper;
public static Caiji Create()
{
if (m_Oper == null)
m_Oper = new Caiji();
return m_Oper;
}
public void Init(WebBrowser w, string text = "")
{
if (w == null)
{
m_Web.DocumentText = text;
}
else
m_Web = w;
Start();
}
private void Start()
{
string Value = string.Empty;
long Id = 0;
HtmlElement hTable = m_Web.Document.GetElementById("J_listMainTable");
HtmlElementCollection hTrs = hTable.GetElementsByTagName("tr");
foreach (HtmlElement tr in hTrs)
{
if (tr.GetElementsByTagName("input").Count > 0)
{
HtmlElementCollection hTds = tr.GetElementsByTagName("td");
Cowry cow = new Cowry();
if (hTds.Count > 0)
{
HtmlElementCollection emTemp = hTds[0].GetElementsByTagName("input");
if (emTemp != null)
{
Value = emTemp[0].GetAttribute("value");
long.TryParse(Value, out Id);
cow.Id = Id;
}
emTemp = hTds[1].GetElementsByTagName("a");
if (emTemp != null && emTemp.Count >= 3)
{
Value = emTemp[0].GetAttribute("href");
cow.t_Link1 = Value;
Value = emTemp[0].InnerText;
cow.Name = Value;
Value = emTemp[2].GetAttribute("href");
cow.t_Link = Value;
}
emTemp = hTds[1].GetElementsByTagName("img");
if (emTemp != null)
{
Value = emTemp[0].GetAttribute("src");
cow.Image = Value;
}
cow.Discount_rate = hTds[2].InnerText;
cow.Price = hTds[3].InnerText;
cow.Commission = hTds[4].InnerText;
cow.Commission_rate = hTds[5].InnerText;
cow.t_30_Num = hTds[6].InnerText;
cow.t_30_Comm = hTds[7].InnerText;
m_Data.Add(cow);
}
}
}
}
}
public class Cowry
{
public long Id { get; set; }
public string Name { get; set; }
public string t_Link { get; set; }
public string t_Link1 { get; set; }
public string Image { get; set; }
public string Price { get; set; }
public string Discount_rate { get; set; }
public string Commission_rate { get; set; }
public string Commission { get; set; }
public string t_30_Num { get; set; }
public string t_30_Comm { get; set; }
}
}
随便写的两句采集阿里妈妈的代码,本来想用正则的,后来想了想登录可能是个问题,干脆就用这个最简单最恶心的办法 WebBrowser来做,未完,该代码仅限于草稿箱