通过WebBrowser C#从DIV标签读取数据 [英] read data from DIV tag via webBrowser C#

查看:86
本文介绍了通过WebBrowser C#从DIV标签读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们
我在表单上有webBrowser1控件
导航网站.
该网站看起来像这样

hi folks
i have webBrowser1 control on my form
navigating website.
and that website looks like this

<html>
<body>
<div class="main">
dont interested this div tag
</div>
<div class="thin">
<a href="http://domain.ge/?showuser=116350">tbiliso_city</a>,
<a href="http://domain.ge/?showuser=56467">GIZ-888</a>,
<a href="http://domain.ge/?showuser=135926">t-90</a>,
</div>
</body>
<html>



http://domain.ge/?showuser=116350是个人资料页面链接,"tbiliso_city"是用户名;
我想阅读div标签"thin"中的所有这些链接(或用户名),并将它们放在列表框中:)
你能帮我吗?

它是我的工作,但不起作用



http://domain.ge/?showuser=116350 is profile page link and "tbiliso_city" is username;
i want to read all of these links (or usernames) in div tag "thin" and put them in listbox :)
can u help me?

its my work but does not work

HtmlDocument doc = webBrowser1.Document;
            HtmlElementCollection col = doc.GetElementsByTagName("thin");

            foreach (HtmlElement element in col)
            {
                string cls = element.GetAttribute("href");

                HtmlElementCollection childDivs = element.Children.GetElementsByName("thin");
                foreach (HtmlElement childElement in childDivs)
                {
                  listBox1.Items.Add(childElement);
                }

推荐答案

引起我注意的第一件事是:<div class="thin">没有定义div元素的名称,它定义了CSS样式类.而是使用属性name(或同时使用这两个属性),例如:<div class="thin" name="thin">.

—SA
First thing which caught my eye is: <div class="thin"> does not define the name of the div element, it defines its CSS style class. Instead, use the attribute name (or both), for example: <div class="thin" name="thin">.

—SA


函数"GetElementsByTagName"将通过标签名称而不是类或ID获取元素.您碰巧正在寻找标签名称为"div"的元素,因此这就是您想要传递给该函数的内容.然后,您需要检查"class"属性(根据我在代码中看到的内容,您已经知道该怎么做)以查看它是否等于"thin".
The function "GetElementsByTagName" will get elements by tag name, not by class or ID. You happen to be looking for elements with the tag name "div", so that''s what you''d want to pass to that function. You''d then want to check the "class" attribute (you already know how to do that, based on what I see in your code) to see if it equals "thin".


感谢我做了
多数民众赞成解决方案:

thanks i did
thats solution:

HtmlElementCollection a = webBrowser1.Document.GetElementsByTagName("a");
            foreach (HtmlElement b in a)
            {
                if (b.GetAttribute("href").StartsWith("http://forum.ge/?showuser="))
                {
                    string item = b.GetAttribute("href");
                    int indx = item.Length - 26;
                    string retString = item.Substring(26, indx);
                    listBox1.Items.Add(retString);
                    item = null;
                    retString = null;
                }
            }


这篇关于通过WebBrowser C#从DIV标签读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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