如何在c#中使用webbrowser对象查找div内容 [英] How to find div content with webbrowser object in c#

查看:426
本文介绍了如何在c#中使用webbrowser对象查找div内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家。

我想在网站上提取数据。

以下网站的源代码:

 <! -    剧集内容 < span class =code-comment>  - >   
< div id = main-content class = >
< span class =code-keyword>< h4 id = episodeTitle > 剧集标题< / h4 >

< p id = episodeDescription > 剧集描述< / p >

< p = 来宾 > 访客< / p >





我的代码是:



  if (webBrowser1.Document!=  null 
{
HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName( episodeDescription);
foreach (HtmlElement elem in elems)
{
String nameStr = elem.GetAttribute( name );
if (nameStr!= null && nameStr.Length!= 0
{
String contentStr = elem.GetAttribute( content);
MessageBox.Show( Document: + webBrowser1.Url.ToString()+ < span class =code-string> \ nDescription: + contentStr);
}
}
}





此代码找不到episodeDescription标签。

如何解决?

谢谢...

解决方案

GetElementsByTagName - 按预期搜索标签!!!

episodeDescription 是元素ID,你应该使用的getElementById ...

Hi Guys.
I want to pull data a website.
The source code for this website below :

<!-- Episode content -->
            <div id="main-content" class="">
                                  <h4 id="episodeTitle">Episode Title</h4>
                
                                  <p id="episodeDescription">Episode Description</p>
                
                                    <p class="guest"> Guests</p>



My code is :

if (webBrowser1.Document != null)
           {
               HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("episodeDescription");
               foreach (HtmlElement elem in elems)
               {
                   String nameStr = elem.GetAttribute("name");
                   if (nameStr != null && nameStr.Length != 0)
                   {
                       String contentStr = elem.GetAttribute("content");
                       MessageBox.Show("Document: " + webBrowser1.Url.ToString() + "\nDescription: " + contentStr);
                   }
               }
           }



This code not find "episodeDescription" tag.
How can I solve it ?
Thanks...

解决方案

GetElementsByTagName - searches, as expected, by tag!!!
episodeDescription is a element id, you should use GetElementById...


这篇关于如何在c#中使用webbrowser对象查找div内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆