WebBrowser 控件 - Document.Body.InnerText 问题

发布于 2024-08-15 11:02:46 字数 2218 浏览 2 评论 0原文

有人可以帮我修复我的代码吗?我看不出我哪里错了。它只是没有做它应该做的事情。

它应该逐行读取一个文件(每行包含 1 个 url),然后 foreach 字符串中的 url 它将访问该 url 并提取标题、url 和正文,然后将其保存到文件中,但它只是不这样做什么都不做。我得到的唯一错误是: “对象引用未设置为对象的实例”,它指向以下代码行:

u = w.Document.Body.InnerText;

这是完整的代码:

    OpenFileDialog of =
        new OpenFileDialog();
    of.Title =
        "app name - Select File";
    using (of)
    {
        try
        {
            Cursor = Cursors.WaitCursor;
            if (of.ShowDialog() == DialogResult.OK)
            {
                string[] file =
                    File.ReadAllLines(
                    of.FileName);


                foreach (string line in file)
                {
                    w.Navigate(line);
                    string t,
                        d,
                        u,
                        path =
                        @"file.txt";

                        t =
                            w.DocumentTitle;
                        u =
                            w.Document.Body.InnerText;
                        d =
                            w.Url.AbsolutePath;
                        t =
                            t.Substring(0,
                            250);
                        t =
                            t.Replace(
                            "\"",
                            "\\\"");

                        a.Text += "\n" +
                            u;

                        File.AppendAllText(path,
                            "s[" +
                            an +
                            "] = \"" +
                            t +
                            "^" +
                            u +
                            "^" +
                            url1 +
                            u +
                            url2 +
                            d +
                            "\";" +
                            Environment.NewLine);
                        an++;
                }
            }
            Cursor = Cursors.Default;
        }
        catch (Exception exception)
        {
            MessageBox.Show(exception.Message);
        }
    }

我非常感谢任何建议/帮助,谢谢:)

jase

Can somebody please help me fix my code.? I can't see where I'm going wrong. It just doesn't do what it should be doing.

It should read a file line by line (every line contains 1 url), and then foreach url in the string it will visit that url and extract title, url, and body text, and then save it to a file but it just doesn't do anything. The only error I am getting is:
"Object reference not set to an instance of an object" which points to the following line of code:

u = w.Document.Body.InnerText;

Here's the full code:

    OpenFileDialog of =
        new OpenFileDialog();
    of.Title =
        "app name - Select File";
    using (of)
    {
        try
        {
            Cursor = Cursors.WaitCursor;
            if (of.ShowDialog() == DialogResult.OK)
            {
                string[] file =
                    File.ReadAllLines(
                    of.FileName);


                foreach (string line in file)
                {
                    w.Navigate(line);
                    string t,
                        d,
                        u,
                        path =
                        @"file.txt";

                        t =
                            w.DocumentTitle;
                        u =
                            w.Document.Body.InnerText;
                        d =
                            w.Url.AbsolutePath;
                        t =
                            t.Substring(0,
                            250);
                        t =
                            t.Replace(
                            "\"",
                            "\\\"");

                        a.Text += "\n" +
                            u;

                        File.AppendAllText(path,
                            "s[" +
                            an +
                            "] = \"" +
                            t +
                            "^" +
                            u +
                            "^" +
                            url1 +
                            u +
                            url2 +
                            d +
                            "\";" +
                            Environment.NewLine);
                        an++;
                }
            }
            Cursor = Cursors.Default;
        }
        catch (Exception exception)
        {
            MessageBox.Show(exception.Message);
        }
    }

I'd appreciate any suggestions/help at all and thank you :)

jase

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长伴 2024-08-22 11:02:46

WebBrowser.Navigate 是 IIRC,异步的。这里使用 WebClient.DownloadString 可能会更好?或 HTML 敏捷包/加载

WebBrowser.Navigate is, IIRC, async. It might be better here to use WebClient.DownloadString? or HTML Agility Pack / Load?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文