0

このコードを使用しています:

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

namespace HtmlParser
{
    public partial class Form1 : Form
    {

        // The HtmlWeb class is a utility class to get the HTML over HTTP
        HtmlWeb htmlWeb = new HtmlWeb();

        // Creates an HtmlDocument object from an URL
        HtmlAgilityPack.HtmlDocument document;

        // Targets a specific node
        HtmlNode someNode;

        public Form1()
        {
            InitializeComponent();
            document = htmlWeb.Load("http://www.walla.co.il");
            someNode = document.GetElementbyId("mynode");

            // If there is no node with that Id, someNode will be null
            if (someNode != null)
            {
                // Extracts all links within that node
                IEnumerable<HtmlNode> allLinks = someNode.Descendants("a");

                // Outputs the href for external links
                foreach (HtmlNode link in allLinks)
                {
                    // Checks whether the link contains an HREF attribute
                    if (link.Attributes.Contains("href"))
                    {
                        // Simple check: if the href begins with "http://", prints it out
                        if (link.Attributes["href"].Value.StartsWith("http://"))
                            richTextBox1.Text = link.Attributes["href"].Value.ToString();
                    }
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }


    }
}

しかし、それは次の行を決して通過しません:

someNode = document.GetElementbyId("mynode");

この行にブレークポイントを使用すると、次のメッセージが表示されます。ソースが利用できませんブレークポイントを使用していない場合、プログラムは実行されていますが、エラーは発生していませんが、機能しません。

私は何をすべきか ?「my node」の代わりに何を入れればよいかわかりませんでした

4

1 に答える 1

2

この問題は、正規表現を使用して HTML を解析しようとしています。

エラーの具体的な原因は、?そこにあってはならない改行文字があり、これにより正規表現が無効になることです。

代わりにHtmlAgilityPackを使用して修正できます。

于 2012-05-09T15:57:23.203 に答える