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

问题描述:

我在制作一个程序时会遇到麻烦,该程序将搜索此网页以获取有关货币市场的最新消息。当我编译程序时,文本框只是空的而没有任何东西发生。有人能帮助我吗?由于我如何获得网页的内容并将其显示在文本框中

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; 
    } 
} 

}

+0

请看看[how-to-ask](http://*.com/help/how-to-ask) – swe

好像你使用的是不正确的组名 - 这是Title,而不是p,根据你的正则表达式。这对我来说产生了“市场新闻标题”:

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