朋友开网店 做个抓取数据的小程序
朋友开网店需要填充初期的数据. 专门做了一个抓取数据的小程序.分享一下.
private void button1_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
string[] sArray=FormatBox(textBox1.Text);
int i = 1;
foreach (string s in sArray)
{
string htm = GetRequestString(s);
string res = FormatHtml(htm);
sb.AppendLine(i.ToString() + "\t" + res);
i++;
}
textBox2.Text = sb.ToString();
using (StreamWriter sw = new StreamWriter(@"c:\test\ouput.txt"))//将获取的内容写入文本
{
sw.Write(sb.ToString());
}
}
protected string[] FormatBox(string Boxtext) {
string[] res = null;
res = Boxtext.Split('\n');
return res;
}
public string FormatHtml(string htm)
{
string res = "";
try
{
string a1 = GetNumCode(htm);
string a2 = GetPrice(htm);
string a3 = GetDeal(htm);
string a4 = GetStuff(htm);
string a5 = GetWoman(htm);
string a6 = GetMan(htm);
string a7 = GetInfo(htm);
res = a1 + "\t" + a2 + "\t" + a3 + "\t" + a4 + "\t" + a5 + "\t" + a6 + "\t" + a7;
}
catch
{
}
return res;
}
public string GetRequestString(string strUrl)
{
string res = "";
try
{
string PageUrl = strUrl;
System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.WebRequest.Create(PageUrl);
request.UserAgent =
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322;.NET CLR 2.0.50727; InfoPath.1) Web-Sniffer/1.0.24";
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream resStream = response.GetResponseStream();
System.IO.StreamReader sr =
new System.IO.StreamReader(resStream, System.Text.Encoding.Default);
res = sr.ReadToEnd();
resStream.Close();
sr.Close();
}
catch { }
return res;
}
/// <summary>
/// 匹配编号
/// </summary>
/// <param name="strSomeCodes"></param>
/// <returns></returns>
public string GetNumCode(string strSomeCodes)
{
Regex DoubleQuotedString = new Regex("bgcolor=\'\\#FFEEFD\'>(\\w*)");
// 然后去匹配字符串。
Match m;
m = DoubleQuotedString.Match(strSomeCodes);
return m.Groups[1].Captures[0].Value;
}
/// <summary>
/// 匹配市场价格
/// </summary>
/// <param name="strSomeCodes"></param>
/// <returns></returns>
public string GetPrice(string strSomeCodes)
{
Regex DoubleQuotedString = new Regex(" class=goodsmoney><s>(\\S*)</s>");
// 然后去匹配字符串。
Match m;
m = DoubleQuotedString.Match(strSomeCodes);
return m.Groups[1].Captures[0].Value;
}
/// <summary>
/// 表面处理
/// </summary>
/// <param name="strSomeCodes"></param>
/// <returns></returns>
public string GetDeal(string strSomeCodes)
{
Regex DoubleQuotedString = new Regex("<td height='25' bgcolor='\\#ffffff'>([^<]*)</td>");
// 然后去匹配字符串。
Match m;
m = DoubleQuotedString.Match(strSomeCodes);
return m.Groups[1].Captures[0].Value;
}
/// <summary>
/// 匹配材质
/// </summary>
/// <param name="strSomeCodes"></param>
/// <returns></returns>
public string GetStuff(string strSomeCodes)
{
Regex DoubleQuotedString = new Regex("<td height='25' bgcolor='\\#ffffff'>([^<]*)</td>");
// 然后去匹配字符串。
Match m;
m = DoubleQuotedString.Match(strSomeCodes);
m = m.NextMatch();
return m.Groups[1].Captures[0].Value;
}
/// <summary>
/// 女戒尺寸
/// </summary>
/// <param name="strSomeCodes"></param>
/// <returns></returns>
public string GetWoman(string strSomeCodes)
{
Regex DoubleQuotedString = new Regex("<td height='25' bgcolor='\\#ffffff'>([^<]*)</td>");
// 然后去匹配字符串。
Match m;
m = DoubleQuotedString.Match(strSomeCodes);
m = m.NextMatch().NextMatch();
return m.Groups[1].Captures[0].Value;
}
/// <summary>
/// 男戒尺寸
/// </summary>
/// <param name="strSomeCodes"></param>
/// <returns></returns>
public string GetMan(string strSomeCodes)
{
Regex DoubleQuotedString = new Regex("<td height='25' bgcolor='\\#ffffff'>([^<]*)</td>");
// 然后去匹配字符串。
Match m;
m = DoubleQuotedString.Match(strSomeCodes);
m = m.NextMatch().NextMatch().NextMatch();
return m.Groups[1].Captures[0].Value;
}
/// <summary>
/// 介绍
/// </summary>
/// <param name="strSomeCodes"></param>
/// <returns></returns>
public string GetInfo(string strSomeCodes)
{
Regex DoubleQuotedString = new Regex("</DIV>\\s*<FONT.>([^/td]*)<\\/FONT>");
// 然后去匹配字符串。
Match m;
m = DoubleQuotedString.Match(strSomeCodes);
return m.Groups[1].Captures[0].Value.Replace(@"<br>", "");
}
{
StringBuilder sb = new StringBuilder();
string[] sArray=FormatBox(textBox1.Text);
int i = 1;
foreach (string s in sArray)
{
string htm = GetRequestString(s);
string res = FormatHtml(htm);
sb.AppendLine(i.ToString() + "\t" + res);
i++;
}
textBox2.Text = sb.ToString();
using (StreamWriter sw = new StreamWriter(@"c:\test\ouput.txt"))//将获取的内容写入文本
{
sw.Write(sb.ToString());
}
}
protected string[] FormatBox(string Boxtext) {
string[] res = null;
res = Boxtext.Split('\n');
return res;
}
public string FormatHtml(string htm)
{
string res = "";
try
{
string a1 = GetNumCode(htm);
string a2 = GetPrice(htm);
string a3 = GetDeal(htm);
string a4 = GetStuff(htm);
string a5 = GetWoman(htm);
string a6 = GetMan(htm);
string a7 = GetInfo(htm);
res = a1 + "\t" + a2 + "\t" + a3 + "\t" + a4 + "\t" + a5 + "\t" + a6 + "\t" + a7;
}
catch
{
}
return res;
}
public string GetRequestString(string strUrl)
{
string res = "";
try
{
string PageUrl = strUrl;
System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.WebRequest.Create(PageUrl);
request.UserAgent =
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322;.NET CLR 2.0.50727; InfoPath.1) Web-Sniffer/1.0.24";
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream resStream = response.GetResponseStream();
System.IO.StreamReader sr =
new System.IO.StreamReader(resStream, System.Text.Encoding.Default);
res = sr.ReadToEnd();
resStream.Close();
sr.Close();
}
catch { }
return res;
}
/// <summary>
/// 匹配编号
/// </summary>
/// <param name="strSomeCodes"></param>
/// <returns></returns>
public string GetNumCode(string strSomeCodes)
{
Regex DoubleQuotedString = new Regex("bgcolor=\'\\#FFEEFD\'>(\\w*)");
// 然后去匹配字符串。
Match m;
m = DoubleQuotedString.Match(strSomeCodes);
return m.Groups[1].Captures[0].Value;
}
/// <summary>
/// 匹配市场价格
/// </summary>
/// <param name="strSomeCodes"></param>
/// <returns></returns>
public string GetPrice(string strSomeCodes)
{
Regex DoubleQuotedString = new Regex(" class=goodsmoney><s>(\\S*)</s>");
// 然后去匹配字符串。
Match m;
m = DoubleQuotedString.Match(strSomeCodes);
return m.Groups[1].Captures[0].Value;
}
/// <summary>
/// 表面处理
/// </summary>
/// <param name="strSomeCodes"></param>
/// <returns></returns>
public string GetDeal(string strSomeCodes)
{
Regex DoubleQuotedString = new Regex("<td height='25' bgcolor='\\#ffffff'>([^<]*)</td>");
// 然后去匹配字符串。
Match m;
m = DoubleQuotedString.Match(strSomeCodes);
return m.Groups[1].Captures[0].Value;
}
/// <summary>
/// 匹配材质
/// </summary>
/// <param name="strSomeCodes"></param>
/// <returns></returns>
public string GetStuff(string strSomeCodes)
{
Regex DoubleQuotedString = new Regex("<td height='25' bgcolor='\\#ffffff'>([^<]*)</td>");
// 然后去匹配字符串。
Match m;
m = DoubleQuotedString.Match(strSomeCodes);
m = m.NextMatch();
return m.Groups[1].Captures[0].Value;
}
/// <summary>
/// 女戒尺寸
/// </summary>
/// <param name="strSomeCodes"></param>
/// <returns></returns>
public string GetWoman(string strSomeCodes)
{
Regex DoubleQuotedString = new Regex("<td height='25' bgcolor='\\#ffffff'>([^<]*)</td>");
// 然后去匹配字符串。
Match m;
m = DoubleQuotedString.Match(strSomeCodes);
m = m.NextMatch().NextMatch();
return m.Groups[1].Captures[0].Value;
}
/// <summary>
/// 男戒尺寸
/// </summary>
/// <param name="strSomeCodes"></param>
/// <returns></returns>
public string GetMan(string strSomeCodes)
{
Regex DoubleQuotedString = new Regex("<td height='25' bgcolor='\\#ffffff'>([^<]*)</td>");
// 然后去匹配字符串。
Match m;
m = DoubleQuotedString.Match(strSomeCodes);
m = m.NextMatch().NextMatch().NextMatch();
return m.Groups[1].Captures[0].Value;
}
/// <summary>
/// 介绍
/// </summary>
/// <param name="strSomeCodes"></param>
/// <returns></returns>
public string GetInfo(string strSomeCodes)
{
Regex DoubleQuotedString = new Regex("</DIV>\\s*<FONT.>([^/td]*)<\\/FONT>");
// 然后去匹配字符串。
Match m;
m = DoubleQuotedString.Match(strSomeCodes);
return m.Groups[1].Captures[0].Value.Replace(@"<br>", "");
}