自动生成文章与抓取网页文章
try { TreeNode tr = treeView1.SelectedNode; //父节点 为了判断选中的是哪个节点才定义的这个变量 //如果我选的父节点是第一个,并且子节点也是第一个,就从数据库里随机生成一篇符合子节点是第一个条件的论文 if (tr.Parent.Index == 0 && treeView1.SelectedNode.Index == 0)//经管类审计 //tr.Parent.Index==0 父节点的索引 { textBox1.Enabled = true; Random r = new Random(); int a = r.Next(1, 6); if (a == 3) { a = r.Next(1, 6); } else { string code = a.ToString(); lunwenData s = new lunwenDA().Select(code); textBox1.Text = s.Zhengwen; MessageBox.Show("生成成功"); } } else if (tr.Parent.Index == 0 && treeView1.SelectedNode.Index == 2)//经管类会计 { textBox1.Enabled = true; Random r = new Random(); int a = r.Next(6, 9); string code = a.ToString(); lunwenData s = new lunwenDA().Select(code); textBox1.Text = s.Zhengwen; MessageBox.Show("生成成功"); } else { textBox1.Enabled = true; MessageBox.Show("暂时还没有论文"); } } catch (Exception) { MessageBox.Show("请先选择你想要生成的论文类型"); }
private void button1_Click(object sender, EventArgs e) { try { #region //HttpWebRequest 网页请求 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("" + textBox2.Text + ""); //HttpWebResponse 网页回应 HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); //Stream 流 //若成功取得网页的内容,则以System.IO.Stream形式返回, Stream stream = webResponse.GetResponseStream(); //将回应的内容存到流里面 StreamReader path = new StreamReader(stream, Encoding.Default); //将流的内容存到path里面 //textBox1.Text = path.ReadToEnd(); #endregion //下面开始截取字符串,抓取文章 string x = path.ReadToEnd(); //文章标题 //string y = x.Substring(x.IndexOf("<h1 class='title'>"), (x.IndexOf("</h1>") - x.IndexOf( "<h1 class='title'>"))); string zhengwen = x.Substring(x.IndexOf("<strong>"), (x.LastIndexOf("<strong>") - x.IndexOf("<strong>")));//从第几位开始截,截多长 string qq = RemoveTags(zhengwen);//去除各种标签 textBox1.Text = qq; } catch (Exception) { MessageBox.Show("请确保网络连接没有问题"); }