利用c#从网上爬取成语的解释

利用c#从网上爬取成语的解释


一年前写的代码,今天整理文件夹时偶然发现,代码写的很糟糕,原本打算删掉的,但又想到当时两眼昏沉地熬夜编代码,心中生出了一丝不舍,今天把它放到这里,就当是留个纪念吧!
代码的功能是从本地的txt成语文档中提取成语,利用c#的 WebRequest从网页中爬取程序的解释并分别存入txt文档和access数据库中。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Net;
namespace 成语查询v1._0
{
    public partial class Form1 : Form
    {
        //Boolean accessFlag = false;//标记是否要存到数据库里面
        //int radioFlag =1;//=1 radioButton1选中,=2 radioButton2选中
        int num = 0;
        string path="";//成语源的地址
        string dbName;//数据库名字
        StreamWriter streamWriter;
        StreamReader fileReader;
        string accessPath;
        OleDbConnection connect;
        public Form1()
        {
            InitializeComponent();
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "" || textBox2 == null)
            {
                MessageBox.Show("请输入txt文档的名字!");
                return;
            }

            //if (File.Exists(textBox2.Text + ".txt"))
               //streamWriter = File.CreateText(textBox2.Text + ".txt");
            streamWriter = new StreamWriter(File.OpenWrite(textBox2.Text + ".txt"),Encoding.UTF8);
            ADOX.Catalog catalog = new ADOX.Catalog();
            if (textBox3.Text == null || textBox3.Text== "")
            {
                MessageBox.Show("请输入数据库的名字!");
                return;
            }
            dbName = textBox3.Text + ".mdb";
            if (File.Exists(dbName))
            {
                accessPath = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName;
                connect = new OleDbConnection(accessPath);
                connect.Open();
            }
            else
            {
                accessPath = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName + ";Jet OLEDB:Engine Type=5";
                try
                {
                    catalog.Create(accessPath);
                    accessPath = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName;
                    connect = new OleDbConnection(accessPath);
                    connect.Open();
                    string command = "create table data (id int primary key ,name char(255) ,content longchar,number1 char(25),number2 char(25))";
                    OleDbCommand dbCommand = new OleDbCommand(command, connect);
                    dbCommand.ExecuteNonQuery();
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.ToString());
                }
            }
            MessageBox.Show("连接本地成功!");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int i = 0;
            int flag = 0;
            if (File.Exists("data.txt"))
            {
                StreamReader data = new StreamReader("data.txt", Encoding.UTF8);
                flag = int.Parse(data.ReadLine());
                data.Close();
            } 
            string name;//成语的名字
            string content;//成语的解释
            string url;
            string format1;
            string format2;
            string result1;//百度上搜索结果的个数
            string result2;//搜狗上搜索结果数
            if (path == "" || path == null)
            {
                MessageBox.Show("请打开成语的源!");
                return;
            }
            num = getColNumber(path);
            try
            {
                fileReader = new StreamReader(path,Encoding.UTF8);
                string command;
                while ((name = fileReader.ReadLine()) != null)
                {
                    if (i > flag)
                    {
                        textBox1.Text = name;
                        if (radioButton1.Checked == true)
                            serchFromWeb1(name, out content);
                        else
                            serchFromWeb2(name, out content);
                        richTextBox1.Text = content;
                        url = @"http://www.baidu.com/s?wd=";
                        format1 = "class=\"nums_text\">百度为您找到相关结果约";
                        format2 = "个</span>";
                        serchNumber(name, url, format1, format2, out result1);
                        textBox4.Text = result1;
                        url = @"https://www.sogou.com/web?query=";
                        format1 = "搜狗已为您找到约";
                        format2 = "条相关结果";
                        serchNumber(name, url, format1, format2, out result2);
                        textBox5.Text = result2;
                        result2 = null;
                        streamWriter.WriteLine(name + "  " + content + " " + result1 + " " + result2, Encoding.ASCII);
                        command = "insert into data(id,name,content,number1,number2) values('" + i + "','" + name + "','" + content + "','" + result1 + "','" + result2 + "')";
                        OleDbCommand dbComman = new OleDbCommand(command, connect);
                        dbComman.ExecuteNonQuery();
                    }
                    i++;
                    progressBar1.Value = i*100 / num;
                }
            }
            catch(Exception ex)
            {
                if(File.Exists("data.txt"))
                    File.Delete("data.txt");
                StreamWriter dataWriter = File.CreateText("data.txt");
                dataWriter.WriteLine(i);
                streamWriter.Close();
                dataWriter.Close();
                fileReader.Close();
                MessageBox.Show("可能是网络出现问题,已经保存进度请求退出");
                return;
            }
            finally
            {
                connect.Close();
                fileReader.Close();
                MessageBox.Show("查询完成!");
            }
              
        }
        /// <summary>
        /// 从百度百科web服务器中的到要查询成语的解释
        /// </summary>
        /// <param name="url"></param>
        /// <param name="name"></param>
        /// <param name="content"></param>
        public void serchFromWeb1(string name,out string content)
        {
            string realUrl = @"https://baike.baidu.com/item/";
            realUrl += name;
            string former1 = "<meta name=\"description\" content=\"";
            string former2 = "...";
            string html;
            int indexS;//formr1的在html中的起始位置
            int indexE;
            int length;
            content = "";
            StreamReader webReader;
            WebRequest webRequest = WebRequest.Create(realUrl);
            WebResponse responds= webRequest.GetResponse();
            Stream stream = responds.GetResponseStream();
            webReader = new StreamReader(stream, Encoding.UTF8);
            html = webReader.ReadToEnd().Substring(0,1000);//通过查看respond得到的html可以知道,词语的解释在1000字以内
            try
            {
                indexS = html.IndexOf(former1, 50);
                indexE = html.IndexOf(former2, 50);
                length = indexE - indexS - former1.Length;
                if (indexS <= 0 || indexE <= 0 || length <= 0 || indexE < indexS)
                {
                    MessageBox.Show("没有在百度百科上找到["+name+"]这个词的解释~~");
                }
                else
                {
                    content = html.Substring(indexS + former1.Length, length);
                }
            }catch(Exception ee)
            {
                MessageBox.Show(ee.ToString());
                return;
            } 
        }
        /// <summary>
        /// 从www.51bc.net网站上获得成语的解释
        /// </summary>
        /// <param name="name"></param>
        /// <param name="content"></param>
        public void serchFromWeb2(string name,out string content)
        {
            try
            {
                WebClient webCilent = new WebClient();
                int indexS;
                int indexE;
                int length;
                string url = @"http://www.51bc.net/cy/serach.php";
                content = "";
                string postString = "f_type=chengyu&f_type2=&f_key=" + name;
                byte[] posDate = Encoding.Default.GetBytes(postString);
                webCilent.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                string resultDate = Encoding.Default.GetString(webCilent.UploadData(url, posDate));
                string html = resultDate.Substring(4460, 1000);
                string format1 = "</u></a></td>";
                string format2 = "</tr>";
                indexS = html.IndexOf(format1) + 22;//从html文档中扣取想要的到的字符串的format1的位置
                indexE = html.IndexOf(format2,indexS) - 9;
                length = indexE - indexS - format1.Length;
                if (indexS <= 0 || indexE <= 0 || length <= 0 || indexE < indexS)
                {
                    MessageBox.Show("没有在51上找到[" + name + "]这个词的解释~~");
                }
                else
                {
                    string result = html.Substring(indexS + format1.Length, length);
                    content = result;
                }   
            }
            catch (Exception ee)
            {
                MessageBox.Show("没有在51上找到[" + name + "]这个词的解释~~");
                content = "";
            }
        }
        /// <summary>
        /// 统计文档中的总行数
        /// </summary>
        /// <param name="thePath"></param>
        /// <returns></returns>
        public int getColNumber(string thePath)
        {
            int i=0;
            StreamReader theReader = new StreamReader(path);
            while (theReader.ReadLine() != null)
            {
                i++;
            }
            theReader.Close();
            return i;
        }
        /// <summary>
        /// 在搜索引擎上搜索的结果数
        /// </summary>
        /// <param name="word"></param>
        /// <param name="result"></param>
        void serchNumber(string word,string url,string format1,string format2, out string result)
        {
            //string webUrl = @"http://www.baidu.com/s?wd=";
            string realUrl = url + word;
            string html;
            int indexS;
            int indexE;
            result = "";
            try
            {
                WebRequest request = WebRequest.Create(realUrl);
                WebResponse respons = request.GetResponse();
                Stream stream = respons.GetResponseStream();
                StreamReader streamReader = new StreamReader(stream, Encoding.UTF8);
                html = streamReader.ReadToEnd().Substring(5000);//随便规定的一个数可能实际意义并不是特别大
                indexS = html.IndexOf(format1);
                indexE = html.IndexOf(format2, indexS);
                result = html.Substring(indexS + format1.Length, indexE - indexS - format1.Length);
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                path = openFileDialog1.FileName;
            } 
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if(radioButton1.Checked == true)
            {
                radioButton2.Checked=false;
                //radioFlag = 1;
            }
                
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked == true)
            {
                radioButton1.Checked = false;
            }
                
        }
    }
}

代码运行结果

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
namespace CYBrowse { #region DateFileHeadStruct /// <summary> /// Defines head struct to read the date file. /// </summary> public struct TagIdiomLibraryHead { public string idiomFlag; //6 byte public int idiomLength; //4 byte public int idiomCount; //3 byte public int[] idiomPositon; //3 byte } #endregion DateFileHeadStruct #region ListViewColumnNameEnum enum ListViewColumnName { ColumnIdiom = 0,//成语 ColumnCode = 1,//拼音简码 ColumnRecordNumber = 2//编号 }; #endregion ListViewColumnNameEnum /// <summary> /// Defines the idiom dictionary main form. /// </summary> public partial class IdiomDictionary : Form { /// <summary> /// The max length of an idiom. /// </summary> const int idiomMaxLength = 36; public const string helpFileName = @"\ReadMe.txt"; /// <summary> /// Declares the idiom dictionary set. /// </summary> public IdiomDictionarySet idiomDictionarySet; /// <summary> /// The index table constructed by the first word in each idiom. /// </summary> public char[] firstWordConstructedIndexTable; /// <summary> /// Represents the index table's count. /// </summary> public int firstWordConstructedIndexTableCount; #region Constructor public IdiomDictionary() { idiomDictionarySet = new IdiomDictionarySet(); InitializeComponent(); } #endregion Constructor /// <summary> /// Invokes this method when IdiomDictionary Form loads. /// </summary> private void IdiomDictionaryLoad(object sender, EventArgs e) { idiomDictionarySet.SetIdiomLibraryBuffer(); firstWordConstructedIndexTable = new char[idiomDictionarySet.idiomLibraryHead.idiomCount]; firstWordConstructedIndexTableCount = 0; listView.Columns.Add("成语",180); listView.Columns.Add("拼音简码", 80); listView.Columns.Add("编号", 60); listView.Sorting = SortOrder.Ascending; listView.BeginUpdate(); for (int i = 0; i < idiomDictionarySet.idiomLibraryHead.idiomCount; i++) { ListViewItem listViewItem= new ListViewItem(); idiomDictionarySet.Move(i); listViewItem.SubItems.Clear(); listViewItem.SubItems[0].Text = (string)idiomDictionarySet.m_CM.Clone(); listViewItem.SubItems.Add(idiomDictionarySet.m_PYCODE); listViewItem.Tag = i; listView.Items.Add(listViewItem); } comboBoxFirst.BeginUpdate(); for (int i = 0; i < idiomDictionarySet.idiomLibraryHead.idiomCount; i++) { listView.Items[i].SubItems.Add((i + 1).ToString()); char ch = listView.Items[i].SubItems[0].Text[0]; if (firstWordConstructedIndexTableCount == 0 || firstWordConstructedIndexTable[firstWordConstructedIndexTableCount - 1] != ch) { firstWordConstructedIndexTable[firstWordConstructedIndexTableCount] = ch; comboBoxFirst.Items.Add(ch); firstWordConstructedIndexTableCount++; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独木春

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值