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

问题描述 投票:-1回答:2

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

我已经探索了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;
        }
    }
}
c# field webpage
2个回答
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
投票

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

var index = sampleString.indexOf('findstring'); 
if(index >= 0) { 
  sampleString = sampleString.Substring(index+9);
}
© www.soinside.com 2019 - 2024. All rights reserved.