从网页中读取特定字段并保存到C#中的字符串

时间:2014-01-20 21:23:56

标签: c# field webpage

我一直试图将某些数据从网页检索到特定字段的字符串,因此我可以发布在我正在开发的应用程序中获取的数据。

我已经探讨了WebClient的使用,但我不确定我是否正在吠叫正确的树来实现这一目标。

你能指点我正确的方向吗?

更新:这就是我所拥有的,但是从这段代码中,我只获得了页面的完整内容,而不是特定的字段:

namespace WebClientExperiments
{
    public partial class Form1 : Form
    {
        Window mainWindow = new Window();

       static WebClient readFromWeb = new WebClient();

        string sampleString = readFromWeb.DownloadString("http://www.google.com");

        public Form1()
        {
            InitializeComponent();
        }

        private void btGet_Click(object sender, EventArgs e)
        {
            tbInfoTxtBox.Text = sampleString;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您可以使用HTMLAGILITYPACK来抓取网页中的数据并检索您想要的特殊标记内容。 从Here下载并在此我可以向您展示一个示例:

using HtmlAgilityPack;


string Price;
HtmlWeb Sitehtml = new HtmlWeb();
HtmlDocument document = new HtmlDocument();
document = Sitehtml.Load(SITE_ADDRESS); // Site address can be like this : http://www.nerkhyab.com
HtmlNode node = document.DocumentNode.SelectSingleNode("//h2");//recognizing Target Node
Price = node.InnerHtml;//put text of target node in variable

答案 1 :(得分:-1)

您应该尝试使用string.indexOf()查找标记在表单中的位置,然后仅使用该索引来读取下一个内容。例如

var index = sampleString.indexOf('findstring'); 
if(index >= 0) { 
  sampleString = sampleString.Substring(index+9);
}