如何获取网页的内容并将其显示在文本框中

时间:2016-10-11 08:20:55

标签: c# .net

我无法制作一个程序来搜索此网页以获取有关货币市场的最新消息。当我编译程序时,文本框只是空的,没有任何反应。有人可以帮帮我吗。感谢

 using System;
using System.Collections.Generic;
  using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
  using System.Net;
 using System.IO;
 using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 using System.Windows.Forms;

namespace WindowsFormsApplication6
{
        public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

        WebClient x = new WebClient();
        string source = x.DownloadString("https://www.dailyfx.com/forex/video/live_events/2016/10/11/Strategy-Video-Opportunity-in-Reconvergence-of-USDCAD-and-Oil-Price-Correlation.html");
        //Get Title
        string title = Regex.Match(source, @"\<p\b[^>]*\>\s*(?<Title>[\s\S]*?)\</p\>", RegexOptions.IgnoreCase).Groups["p"].Value;

        textBox1.Text =title;
    }
}

}

1 个答案:

答案 0 :(得分:0)

根据你的正则表达式,似乎你使用了一个不正确的群组名称 - 它是Title,而不是p。这产生了&#34;市场新闻头条&#34;对我来说:

string title = Regex.Match(source, @"\<p\b[^>]*\>\s*(?<Title>[\s\S]*?)\</p\>", RegexOptions.IgnoreCase).Groups["Title"].Value;