【发布时间】:2023-07-12 07:22:01
【问题描述】:
我正在使用 C# 中的 webBrowser,我需要从链接中获取文本。该链接只是一个没有类的 href。
是这样的
<div class="class1" title="myfirstClass">
<a href="link.php">text I want read in C#
<span class="order-level"></span>
不应该是这样的吗?
HtmlElementCollection theElementCollection = default(HtmlElementCollection);
theElementCollection = webBrowser1.Document.GetElementsByTagName("div");
foreach (HtmlElement curElement in theElementCollection)
{
if (curElement.GetAttribute("className").ToString() == "class1")
{
HtmlElementCollection childDivs = curElement.Children.GetElementsByName("a");
foreach (HtmlElement childElement in childDivs)
{
MessageBox.Show(childElement.InnerText);
}
}
}
【问题讨论】:
-
不知道这个类,但是与 HTML 相关的
GetElementById应该只检索具有该 ID 的 1 个元素,并且这些 ID 不能包含空格。我也没有在 HTML 中看到任何 ID -
我很好奇,你用什么服务器端语言来获取客户端 HTML 的内部文本?
标签: c# html web webrequest