前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >朋友开网店 做个抓取数据的小程序

朋友开网店 做个抓取数据的小程序

作者头像
汤高
发布2018-01-11 17:31:54
1K0
发布2018-01-11 17:31:54
举报
文章被收录于专栏:积累沉淀积累沉淀积累沉淀

朋友开网店需要填充初期的数据.  专门做了一个抓取数据的小程序.分享一下.

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>", "");

        }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2007-04-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档