C# “提取HtmleElement的文本内容”;onclick";带C的属性#
我有这个HTML代码C# “提取HtmleElement的文本内容”;onclick";带C的属性#,c#,html-parsing,webbrowser-control,C#,Html Parsing,Webbrowser Control,我有这个HTML代码 <div class="anc-style" onclick="window.open('./view.php?a=foo')"></div> 理想情况下会产生字符串 “window.open('./view.php?a=foo')” 但它返回一个系统 我可以通过将(“onclick”)更改为(“class”)来获得类,onclick是怎么回事 HtmlElementCollection div = webBrowser1.Document.Get
<div class="anc-style" onclick="window.open('./view.php?a=foo')"></div>
理想情况下会产生字符串
“window.open('./view.php?a=foo')”
但它返回一个系统
我可以通过将(“onclick”)更改为(“class”)来获得类,onclick是怎么回事
HtmlElementCollection div = webBrowser1.Document.GetElementsByTagName("div");
for (int j = 0; j < div.Count; j++) {
if (div[j].GetAttribute("class") == "anc-style") {
richTextBox1.AppendText(div[j].GetAttribute("onclick").ToString());
}
}
HtmlElementCollection div=webBrowser1.Document.GetElementsByTagName(“div”);
对于(int j=0;j
尝试使用div[j][“onclick”]
您使用的浏览器是什么
我已经创建了一个JSFIDLE,它可以工作,请尝试一下,看看它是否适合您
尝试使用
div[j][“onclick”]
您使用的浏览器是什么
我已经创建了一个JSFIDLE,它可以工作,请尝试一下,看看它是否适合您
您可以使用htmlDocument类提取文档标记和以下数据。这只是一个例子
string htmlText = "<html><head></head><body><div class=\"anc-style\" onclick=\"window.open('./view.php?a=foo')\"></div></body></html>";
WebBrowser wb = new WebBrowser();
wb.DocumentText = "";
wb.Document.Write(htmlText);
foreach (HtmlElement hElement in wb.Document.GetElementsByTagName("DIV"))
{
//get start and end positions
int iStartPos = hElement.OuterHtml.IndexOf("onclick=\"") + ("onclick=\"").Length;
int iEndPos = hElement.OuterHtml.IndexOf("\">",iStartPos);
//get our substring
String s = hElement.OuterHtml.Substring(iStartPos, iEndPos - iStartPos);
MessageBox.Show(s);
}
字符串htmlText=”“;
WebBrowser wb=新的WebBrowser();
wb.DocumentText=“”;
wb.Document.Write(htmlText);
foreach(wb.Document.GetElementsByTagName(“DIV”)中的HtmleElement-hElement)
{
//获得起始和结束位置
int-iStartPos=hElement.OuterHtml.IndexOf(“onclick=\”)+(“onclick=\”).Length;
int iEndPos=hElement.OuterHtml.IndexOf(“\”>”,iStartPos);
//获取我们的子字符串
字符串s=hElement.OuterHtml.Substring(iStartPos、iEndPos-iStartPos);
MessageBox.Show(s);
}
您可以使用htmlDocument类拉取文档标记并提取如下数据。这只是一个示例
string htmlText = "<html><head></head><body><div class=\"anc-style\" onclick=\"window.open('./view.php?a=foo')\"></div></body></html>";
WebBrowser wb = new WebBrowser();
wb.DocumentText = "";
wb.Document.Write(htmlText);
foreach (HtmlElement hElement in wb.Document.GetElementsByTagName("DIV"))
{
//get start and end positions
int iStartPos = hElement.OuterHtml.IndexOf("onclick=\"") + ("onclick=\"").Length;
int iEndPos = hElement.OuterHtml.IndexOf("\">",iStartPos);
//get our substring
String s = hElement.OuterHtml.Substring(iStartPos, iEndPos - iStartPos);
MessageBox.Show(s);
}
字符串htmlText=”“;
WebBrowser wb=新的WebBrowser();
wb.DocumentText=“”;
wb.Document.Write(htmlText);
foreach(wb.Document.GetElementsByTagName(“DIV”)中的HtmleElement-hElement)
{
//获得起始和结束位置
int-iStartPos=hElement.OuterHtml.IndexOf(“onclick=\”)+(“onclick=\”).Length;
int iEndPos=hElement.OuterHtml.IndexOf(“\”>”,iStartPos);
//获取我们的子字符串
字符串s=hElement.OuterHtml.Substring(iStartPos、iEndPos-iStartPos);
MessageBox.Show(s);
}
这与C有什么关系#请发布您为实现此目的而创建的代码。添加代码以进一步解释,抱歉。它正在使用webbrowser控件。我认为问题是因为基础IE引擎正在返回“脚本对象”,而GetAttribute
没有正确返回DOM属性(而是它所表示的对象)。虽然这是一次彻底的黑客攻击,但它很可能可以用“字符串”提取出来。我再说一遍:完全的黑客。还可以提取返回的所述“脚本对象”的文本。但是第一步是确定它是什么类型的对象。返回OuterHtml确实有效,尽管在我的例子中,这个div元素也有子元素,所以它也会以文本的形式返回这些子元素。这与C有什么关系?请发布您创建的代码以尝试完成此操作。添加了代码以进一步解释,抱歉。它正在使用webbrowser控件。我认为问题是因为基础IE引擎正在返回“脚本对象”,而GetAttribute
没有正确返回DOM属性(而是它所表示的对象)。虽然这是一次彻底的黑客攻击,但它很可能可以用“字符串”提取出来。我再说一遍:完全的黑客。还可以提取返回的所述“脚本对象”的文本。但是第一步是确定它是什么类型的对象。返回OuterHtml确实有效,尽管在我的例子中,这个div元素也有子元素,所以它也会将这些子元素作为文本返回。这确实有效。非常感谢。我知道OuterHtml会返回文本,但没有想到使用索引。@Durn很高兴提供帮助,别忘了标记您使用的答案。谢谢!!工作完美!这确实有效。非常感谢。我知道OuterHtml会返回文本,但没有想到使用索引。@Durn很高兴提供帮助,别忘了标记您使用的答案。谢谢!!工作完美!这确实有效,但我使用的是C#,而不是javascript。谢谢你的意见!这确实有效,但我使用的是C#,而不是javascript。谢谢你的意见!