http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2413|回复: 4

[其他] FOM1下可以执行代码转移到工作类就不能执行

[复制链接]
发表于 2013-9-2 11:58:02 | 显示全部楼层 |阅读模式
本帖最后由 zjz119 于 2013-9-2 12:05 编辑
  1. [code=csharp]private void button2_Click(object sender, EventArgs e)
  2.         {
  3.             item = new HttpItem
  4.             {
  5.                 Cookie = cookie,
  6.                 URL = (ServerURL + "/user/personal.ql"),
  7.                 Accept = "text/html, application/xhtml+xml, */*",
  8.                 UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
  9.             };
  10.             HttpResult result = http.GetHtml(item);
  11.             string html = result.Html;
  12.             //richTextBox1.AppendText(html + "\r\n\r\n");
  13.             html = Compress(html);
  14.             //richTextBox1.AppendText(html + "\r\n\r\n");
  15.             Match HeroShuLiang = Regex.Match(html, "(?<=英雄数量:</td><td class="n">).*?(?=<)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
  16.             //richTextBox1.AppendText(HeroShuLiang + "\r\n\r\n");
  17.             item.URL = (ServerURL + HerolistURL);
  18.             result = http.GetHtml(item);
  19.             html = result.Html;
  20.             //html = Compress(html);
  21.             //截取英雄列表html
  22.             Match txtHerolist = Regex.Match(html, "(?<=英雄列表 -->).*?(?=英雄信息)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
  23.             //richTextBox1.AppendText(txtHerolist + "\r\n\r\n");
  24.             string _txtHerolist = txtHerolist.Value.ToString();
  25.             richTextBox1.AppendText(_txtHerolist + "\r\n\r\n");
  26.             Regex re = new Regex("<a.*?heroId=(.+?)"><img.*?>LV.(.+?)</span.*?h_name.oit_.">(.+?)<", RegexOptions.IgnoreCase | RegexOptions.Singleline);
  27.             MatchCollection mc = re.Matches(_txtHerolist);
  28.             richTextBox1.AppendText(mc + "\r\n\r\n");
  29.             List<string> idlist = new List<string>();//创建list
  30.             List<string> lvlist = new List<string>();
  31.             List<string> namelist = new List<string>();
  32.             foreach (Match ma in mc)
  33.             {
  34.                 idlist.Add(ma.Groups[1].Value.ToString());//捕获数据加入list
  35.                 lvlist.Add(ma.Groups[2].Value.ToString());
  36.                 namelist.Add(ma.Groups[3].Value.ToString());
  37.             }
  38.             HeroInfoTable = new DataTable("HeroInfo");//加入英雄信息datatable
  39.             string str = "";
  40.             HeroInfoTable.Columns.Add("ID", str.GetType());
  41.             richTextBox1.AppendText(str + "\r\n\r\n");
  42.             HeroInfoTable.Columns.Add("等级", str.GetType());
  43.             HeroInfoTable.Columns.Add("名字", str.GetType());

  44.             string[] Chtml = new string[idlist.Count];
  45.             List<string> tmplist = new List<string>();
  46.             for (int i = 0; i < idlist.Count; i++)
  47.             {
  48.                 tmplist.Clear();
  49.                 tmplist.Add(idlist[i]);
  50.                 tmplist.Add(lvlist[i]);
  51.                 tmplist.Add(namelist[i]);
  52.                 HeroInfoTable.Rows.Add(tmplist.ToArray());
  53.             }
  54.             comboBox1.Items.Clear();
  55.             for (int i = 0; i < HeroInfoTable.Rows.Count; i++)
  56.             {
  57.                 string its = HeroInfoTable.Rows[i][2].ToString() + "-" + HeroInfoTable.Rows[i][1].ToString();
  58.                 comboBox1.Items.Add(its);
  59.             }
  60.         }
复制代码
[/code]
可以正常执行
新建一个InfoHelper.cs类
  1. public string GetHeroInfo()
  2.         {
  3.             item = new HttpItem
  4.             {
  5.                 Cookie = cookie,
  6.                 URL = (ServerURL + "/user/personal.ql"),
  7.                 Accept = "text/html, application/xhtml+xml, */*",
  8.                 UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
  9.             };
  10.             HttpResult result = http.GetHtml(item);
  11.             string html = result.Html;
  12.             //richTextBox1.AppendText(html + "\r\n\r\n");
  13.             html = Compress(html);
  14.             //richTextBox1.AppendText(html + "\r\n\r\n");
  15.             Match HeroShuLiang = Regex.Match(html, "(?<=英雄数量:</td><td class="n">).*?(?=<)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
  16.             //richTextBox1.AppendText(HeroShuLiang + "\r\n\r\n");
  17.             item.URL = (ServerURL + HerolistURL);
  18.             result = http.GetHtml(item);
  19.             html = result.Html;
  20.             //html = Compress(html);
  21.             //截取英雄列表html
  22.             Match txtHerolist = Regex.Match(html, "(?<=英雄列表 -->).*?(?=英雄信息)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
  23.             //richTextBox1.AppendText(txtHerolist + "\r\n\r\n");
  24.             string _txtHerolist = txtHerolist.Value.ToString();
  25.             //richTextBox1.AppendText(_txtHerolist + "\r\n\r\n");
  26.             Regex re = new Regex("<a.*?heroId=(.+?)"><img.*?>LV.(.+?)</span.*?h_name.oit_.">(.+?)<", RegexOptions.IgnoreCase | RegexOptions.Singleline);
  27.             MatchCollection mc = re.Matches(_txtHerolist);
  28.             //richTextBox1.AppendText(mc + "\r\n\r\n");
  29.             List<string> idlist = new List<string>();//创建list
  30.             List<string> lvlist = new List<string>();
  31.             List<string> namelist = new List<string>();
  32.             foreach (Match ma in mc)
  33.             {
  34.                 idlist.Add(ma.Groups[1].Value.ToString());//捕获数据加入list
  35.                 lvlist.Add(ma.Groups[2].Value.ToString());
  36.                 namelist.Add(ma.Groups[3].Value.ToString());
  37.             }
  38.             HeroInfoTable = new DataTable("HeroInfo");//加入英雄信息datatable
  39.             string str = "";
  40.             HeroInfoTable.Columns.Add("ID", str.GetType());
  41.             HeroInfoTable.Columns.Add("等级", str.GetType());
  42.             HeroInfoTable.Columns.Add("名字", str.GetType());

  43.             string[] Chtml = new string[idlist.Count];
  44.             List<string> tmplist = new List<string>();
  45.             for (int i = 0; i < idlist.Count; i++)
  46.             {
  47.                 tmplist.Clear();
  48.                 tmplist.Add(idlist[i]);
  49.                 tmplist.Add(lvlist[i]);
  50.                 tmplist.Add(namelist[i]);
  51.                 HeroInfoTable.Rows.Add(tmplist.ToArray());
  52.             }
  53.             return "获取英雄信息成功!";
  54.         }
复制代码
Form1改成
  1. private void button2_Click(object sender, EventArgs e)
  2.         {
  3.             InfoHelper _InfoHelper = new InfoHelper();
  4.             comboBox1.Items.Clear();
  5.             for (int i = 0; i < _InfoHelper.HeroInfoTable.Rows.Count; i++)
  6.             {
  7.                 string its = _InfoHelper.HeroInfoTable.Rows[i][2].ToString() + "-" + _InfoHelper.HeroInfoTable.Rows[i][1].ToString();
  8.                 comboBox1.Items.Add(its);
  9.             }
  10.         }
复制代码
报发生了未经处理的异常
未将对象引用设置到对象实例。
这是哪里出错了?


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-9-2 12:00:14 | 显示全部楼层
不能执行是什么问题,比如运行不正确,还是报什么错,这不能执行叫人难以理解
 楼主| 发表于 2013-9-2 12:05:32 | 显示全部楼层
刚才发的有问题重新编辑了一下!麻烦指导一下
发表于 2013-9-2 12:51:01 | 显示全部楼层
调试一看就知道了,
HeroInfoTable应该是这样没有数据
调试的方法参考http://www.sufeinet.com/forum.ph ... hlight=%B5%F7%CA%D4
 楼主| 发表于 2013-9-2 13:59:28 | 显示全部楼层
问题已解决
原来少了一行_InfoHelper.GetHeroInfo();
  1. private void button2_Click(object sender, EventArgs e)
  2.         {
  3.             _InfoHelper.GetHeroInfo();
  4.             comboBox1.Items.Clear();
  5.             for (int i = 0; i < _InfoHelper.HeroInfoTable.Rows.Count; i++)
  6.             {
  7.                 string its = _InfoHelper.HeroInfoTable.Rows[i][2].ToString() + "-" + _InfoHelper.HeroInfoTable.Rows[i][1].ToString();
  8.                 comboBox1.Items.Add(its);
  9.             }
  10.         }
复制代码
这样就行了!
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-2 03:10

© 2014-2021

快速回复 返回顶部 返回列表