简体   繁体   中英

Using “stringbyevaluatingjavascriptfromstring” for get the link in <a href='XXXX'> tag in objective C

I have html code like this

<html>
  <head>
  </head>
  <body>
    <a href='001'><img src='chp1.png'/></a>
    <a href='002'><img src='chp2.png'/></a>
</body></html>

I want to get 001 or 002 in by use stringbyevaluatingjavascriptfromstring

like

[self.webView stringByEvaluatingJavaScriptFromString:@""];

have anyone know how to get it

Thx

I don't think -stringByEvaluatingJavaScriptFromString: is a good choice here. Try using an NSScanner :

NSScanner* scanner = [NSScanner scannerWithString:yourHTML];
while ([scanner scanUpToString:@"a href='" intoString:nil])
{
    [scanner scanString:@"a href='" intoString:nil];
    NSString* result = nil;
    [scanner scanUpToString:@"'" intoString:&result];
    //Do something with result, which will equal 001, then 002, etc.
}

Note that if you need to parse HTML you didn't write yourself, you'll need to add a lot more flexibility to this to account for possible stylistic differences (eg ' vs " , href vs HREF ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM