【问题标题】:Read specific fields from webpage and save to string in C#从网页中读取特定字段并保存到 C# 中的字符串
【发布时间】:2014-01-20 21:23:55
【问题描述】:

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

我已经探索了 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;
        }
    }
}

【问题讨论】:

  • 请展示您尝试过的内容并详细说明您在编写代码时遇到的问题。
  • 您正在寻找 HTML 敏捷包。
  • 你的意思是你想废弃一个网页并从那个页面的特殊标签中的一个html标签中检索一些数据或文本?

标签: c# field webpage


【解决方案1】:

您可以使用 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

【讨论】:

  • 注意:站点地址必须与包含你的目标标签的网页地址完全相同我的目标标签是“h2”标签的文本
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2018-05-13
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    相关资源
    最近更新 更多