0

I need to get URL from loaded HTML page. Here is HTML tag where placed my URL

<a class="top_nav_link" id="logout_link" href="https://login.vk.com/?act=logout&hash=29327318c645d49a48&from_host=vk.com&from_protocol=http" onclick="if (!checkEvent(event)) { ge('logout_form').submit(); return false; }">

And URL: "https://login.vk.com/?act=logout&hash=29327318c645d49a48&from_host=vk.com&from_protocol=http"

Hash could be different. How to get this URL?

Timur Mustafaev
  • 4,869
  • 9
  • 63
  • 109

1 Answers1

1

Since you say the HTML is actually well-formed XHTML, then you can use any XML parsing method to parse the document and find what you are looking for. Using NSXMLParser and a valid parser delegate, you probably would have something like:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {

    if ([elementName isEqualToString:@"a"] && [[attributeDict objectForKey:@"id"] isEqualToString:@"logoutLink"]) {
        // Found the <a> tag with an id of logoutLink
        NSString *linkURL = [attributeDict objectForKey:@"href"];
        // Do what you want with the link URL here
    }

This assumes you are looking for a specific <a> element with an id of logoutLink. If you are looking for other ways to identify which <a> tag has the URL you want you can adjust the if statement in this sample code accordingly.

Tim Dean
  • 8,253
  • 2
  • 32
  • 59
  • I tried to make it, but parser stops at tag (before it succesfully parsed 5 more tags). Maybe I was mistaken when said that page is well-formated XHTML. – Timur Mustafaev Feb 27 '12 at 20:12
  • To check if it is valid XHTML, try using a validator site such as http://validator.w3.org/ – Tim Dean Feb 27 '12 at 20:16
  • In that case, you might want to try a library like lib tidy to first cleanup your HTML. See http://stackoverflow.com/q/1527883/830760 for another SO question with some links to libtidy – Tim Dean Feb 27 '12 at 20:39