Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get href attributes of a tags in this string?

Tags:

html

string

c#

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>
like image 782
hmahdavi Avatar asked Jan 17 '16 06:01

hmahdavi


People also ask

How do I get the href of a string?

If you have a reference to the element, you can get the href from it like this: var link = theElement. href; // or var link = theElement. getAttribute("href");

What method can be used to retrieve the href attribute of an element?

Answer: Use the jQuery . attr() Method attr() method to dynamically set or change the value of href attribute of a link or anchor tag. This method can also be used to get the value of any attribute.

Which tag is used for href attribute?

Attributes should always be applied with start tag. The Attribute should always be applied with its name and value pair.


3 Answers

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.

like image 188
Guy Avatar answered Oct 01 '22 18:10

Guy


You can use Html Agility Pack

Html Agility Pack Examples:

 HtmlDocument doc = new HtmlDocument();
 doc.Load("file.htm");
 foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
 {
    HtmlAttribute att = link["href"];
    att.Value = FixLink(att);
 }
 doc.Save("file.htm");

Links :

How to use HTML Agility pack

http://www.mikesdotnetting.com/article/273/using-the-htmlagilitypack-to-parse-html-in-asp-net http://www.codeproject.com/Articles/691119/Html-Agility-Pack-Massive-information-extraction-f

I hope this information will help

like image 36
Ahmed Galal Avatar answered Oct 05 '22 18:10

Ahmed Galal


For better understanding

Substring(a,b)

  • a : from where you want to start your Substring
  • b : what will be the length of the Substring

In your ex you take:

a as start index of ul

b as end index of ul // Error b will be the length from string start to end of ul!

you need to do is:

int c = b - a // (will give you the inner text length)

_codeHtml = _codeHtml.Substring(a,c);
like image 35
Leon Barkan Avatar answered Oct 03 '22 18:10

Leon Barkan