WebBrowser 控件 - Document.Body.InnerText 问题
有人可以帮我修复我的代码吗?我看不出我哪里错了。它只是没有做它应该做的事情。
它应该逐行读取一个文件(每行包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WebBrowser.Navigate
是 IIRC,异步的。这里使用WebClient.DownloadString
可能会更好?或 HTML 敏捷包/加载
?WebBrowser.Navigate
is, IIRC, async. It might be better here to useWebClient.DownloadString
? or HTML Agility Pack /Load
?