代码人生的小狗窝

一行行枯燥的代码,却描绘出人生的点点滴滴

您现在的位置是:首页>_C#

怎么用c#编写程序,读入一个网站的网页,查找其中的字符,并输入

发布时间:2019-08-20浏览(546)

    如何用c#编写程序,读入一个网站的网页,查找其中的字符,并输入?
    如何用c#编写程序,读入一个网站的网页,查找其中的字符,并输入?

    最近看论坛,好多人跟帖,但是作者的发言好难找
    能不能编写段代码,来自动列出作者的发言?


    思路:
    打开网页
    读入此网页的源文件
    搜索此文件的作者名及发言

    但是,如何使用c#读入此网页源文件呢?
    高手请指教!!!!!

    ------解决方案--------------------
    我觉得你这个就是一个小偷程序吧
    public void show5()
    {
    Encoding gb2312 = Encoding.GetEncoding("gb2312");
    String web_url = "http://china.fxstreet.com/fundamental/economic-calendar/";
    //要获取的网址URL
    String code = String.Empty;
    //存放网页的源文件
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(web_url);
    WebResponse response = request.GetResponse();
    StreamReader sr = new StreamReader(response.GetResponseStream(), gb2312);
    //获取源文件
    code = sr.ReadToEnd();
    sr.Close();
    ArrayList list = new ArrayList();
    //用来存放链接
    String reg = @"<td align=.left.>\d*:\d*</td><td>\D*</td><td><a.*return false;.>.*</a></td>.*<img.*</td><td id=.*</td><td>.*nowrap=.nowrap.>.*</td>";
    String reg1 = @"<td align=.left.>\d*:\d*</td>";
    String reg2 = @"<td>\D*</td><td><a";
    String reg3 = @"return false;.>.*</a></td>";
    String reg4 = @"</a></td><td><img.*alt=.* expected. /></td>";
    String reg5 = @"<td id=.*</td><td>.*</td><td nowrap";
    String reg6 = @"nowrap=.nowrap.>.*</td><td><img";
    //链接的正则表达式
    Regex regex = new Regex(reg, RegexOptions.IgnoreCase);
    MatchCollection mc = regex.Matches(code);
    //存放匹配的集合
    for (int i = 0; i < mc.Count; i++)
    {
    Regex regex1 = new Regex(reg1, RegexOptions.IgnoreCase);
    MatchCollection mc1 = regex1.Matches(mc[i].Value.ToString());
    Regex regex2 = new Regex(reg2, RegexOptions.IgnoreCase);
    MatchCollection mc2 = regex2.Matches(mc[i].Value.ToString());
    Regex regex3 = new Regex(reg3, RegexOptions.IgnoreCase);
    MatchCollection mc3 = regex3.Matches(mc[i].Value.ToString());
    Regex regex4 = new Regex(reg4, RegexOptions.IgnoreCase);
    MatchCollection mc4 = regex4.Matches(mc[i].Value.ToString());
    Regex regex5 = new Regex(reg5, RegexOptions.IgnoreCase);
    MatchCollection mc5 = regex5.Matches(mc[i].Value.ToString());
    Regex regex6 = new Regex(reg6, RegexOptions.IgnoreCase);
    MatchCollection mc6 = regex6.Matches(mc[i].Value.ToString());
    bool hasExist = false;
    //链接存在与否的标记
    if (mc1.Count < 1)
    {
    continue;
    }
    String ctime = mc1[0].Value;
    String carea = mc2[0].Value;
    String ctitle = mc3[0].Value;
    String ccurrent = mc4[0].Value;
    String cforecast = mc5[0].Value;
    String clast = mc6[0].Value;
    ctime = ctime.Substring(ctime.Length - 10, 5);
    carea = carea.Replace("</td>", "");
    carea = carea.Replace("<td>", "");
    carea = carea.Replace("<a", "");
    ctitle = ctitle.Replace("return false;\">", "");
    ctitle = ctitle.Replace("</a></td>", "");