如何获得此字符串中标签的href属性? [英] How to get href attributes of a tags in this string?

查看:92
本文介绍了如何获得此字符串中标签的href属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  http:/ /> 

/bipardeh94.blogfa.comtarget =_ blank

http://avaejam.blogfa.comtarget =_ blank

和...
我想用C#做这个。怎么做到这一点?
我使用这段代码,但是这并不完整。

  int indexStartUl = _codeHtml.IndexOf(< ul ); 
int indexEndUl = _codeHtml.IndexOf(< / ul>);
_codeHtml = _codeHtml.Substring(indexStartUl,indexEndUl);

请帮助。

 < ul class =ull> 
< li>< a href =http://bipardeh94.blogfa.comtarget =_ blank>باغبلور< / a>< span class =ur> bipardeh94.blogfa .com< / span>< span class =ds>فرهنگی-خبری-علمی< / span>< / li>
< li>< a href =http://avaejam.blogfa.comtarget =_ blank>免费试用< / a>< span class =ur> avaejam。 blogfa.com< / span>< span class =ds> نتا&&&&/< / span>< / li>
< li>< a href =http://prkangavar.blogfa.comtarget =_ blank>روابطعمومیدانشگاهآزاداسلامیکنگاور< / a>< span class =ur> prkangavar.blogfa.com< / span>< span class =ds>اخباردانشگاه< / span>< / li>
< li>< a href =http://bordekhoun.blogfa.comtarget =_ blank>وبلاگاطلاعرسانیبردخون< / a>< span class =ur> bordekhoun .blogfa.com< / span>< span class =ds>اخباروگزارشاتوتحلیلهادربارهبردخون< / span>< / li>
< li>< a href =http://mahinvare.blogfa.comtarget =_ blank>تدوریهاینوین< / a>< span class =ur> mahinvare。 blogfa.com< / span>< span class =ds>您可以在< / span>< / li>中找到< / span>
< li>< a href =http://zanjanuniversity.blogfa.comtarget =_ blank>دانشگاهزنجان< / a>< span class =ur> zanjanuniversity.blogfa .com< / span>< span class =ds>将您的电子邮件地址发送给您的邮箱< / span>
< / li>
< / ul>


解决方案

您可以使用 Selenium WebDriver 功能:

  IList< IWebElement> lis = driver.FindElements(By.CssSelector(.ll>> li)); 
foreach(IWebElement li in lis){
string href = li.GetAttribute(href);
}

您可以找到所有 WebElements with li 标记,它们是 WebElement 的子类 ull ,并遍历列表并获取href属性。

In this string exist number li tag .I want get href attribute of a tags such as this:

http://bipardeh94.blogfa.com" target="_blank

http://avaejam.blogfa.com" target="_blank

and ... I want that do this with C# .How to done this? I use this code but this is not complete .

int indexStartUl = _codeHtml.IndexOf("<ul");
            int indexEndUl = _codeHtml.IndexOf("</ul>");
            _codeHtml = _codeHtml.Substring(indexStartUl, indexEndUl);

Please help.

 <ul class="ull">
        <li><a href="http://bipardeh94.blogfa.com" target="_blank">باغ بلور</a><span class="ur">bipardeh94.blogfa.com</span><span class="ds">فرهنگی-خبری-علمی</span></li>
        <li><a href="http://avaejam.blogfa.com" target="_blank">هزار نکته </a><span class="ur">avaejam.blogfa.com</span><span class="ds"> يك نكته از هزار نكته  باشد تا بعد </span></li>
        <li><a href="http://prkangavar.blogfa.com" target="_blank">روابط عمومی دانشگاه آزاداسلامی کنگاور</a><span class="ur">prkangavar.blogfa.com</span><span class="ds">اخبار دانشگاه</span></li>
        <li><a href="http://bordekhoun.blogfa.com" target="_blank">وبلاگ اطلاع رسانی بردخون</a><span class="ur">bordekhoun.blogfa.com</span><span class="ds">اخباروگزارشات وتحلیل ها درباره بردخون</span></li>
        <li><a href="http://mahinvare.blogfa.com" target="_blank">تدوری های نوین</a><span class="ur">mahinvare.blogfa.com</span><span class="ds">نظریه های علوم انسانی باید متحول شود</span></li>
        <li><a href="http://zanjanuniversity.blogfa.com" target="_blank">دانشگاه زنجان</a><span class="ur">zanjanuniversity.blogfa.com</span><span class="ds">اخبار دانشگاهیان زنجان و دانشگاه آزاد زنجان و سیستم ثبت نام شهردای زنجان </span>
        </li>
    </ul>

解决方案

You can use Selenium WebDriver functionality:

IList<IWebElement> lis = driver.FindElements(By.CssSelector(".ull > li"));
foreach (IWebElement li in lis) {
    string href = li.GetAttribute("href");
}

You find all WebElements with li tag who are children of WebElement with class ull, and the iterate over the list and take the href attribute.

这篇关于如何获得此字符串中标签的href属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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