如何从ios中的html文档中提取“meta [content $ =。mp4] / content”?

时间:2017-04-04 13:25:41

标签: objective-c ios9 tfhpple

我正在使用hpple库来解析HTML文档。我想为视频网址提取.mp4链接和图像链接。 Html标签看起来像这样:

<meta property="og:video" content="http://instagram.fbom1-1.fna.fbcdn.net/t50.2886-16/17686957_372561729804133_1618562720464896000_n.mp4">

代码:

NSURL *instagramUrl = [NSURL URLWithString:@"https://www.instagram.com/p/BSdePkEFfO-/?tagged=video&hl=en"];
NSData *htmlData = [NSData dataWithContentsOfURL: instagramUrl];

TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSArray *elements  = [xpathParser  searchWithXPathQuery:@"//div[@id='preview']"]; 
TFHppleElement *element = [elements objectAtIndex:0];
NSString *string = [element content];
NSLog(@"%@", string);

我总是把TFHppleElement弄空。我不知道这个人,请帮忙。

谢谢!

1 个答案:

答案 0 :(得分:0)

//获取.jpg链接

 NSURL *cUrl = [NSURL URLWithString:url1];
    NSData *cHtmlData = [NSData dataWithContentsOfURL:cUrl];
    TFHpple *cParser = [TFHpple hppleWithHTMLData:cHtmlData];
    NSArray *elements = [cParser searchWithXPathQuery:@"//meta[@property='og:image']"];
    TFHppleElement *element = [elements objectAtIndex:0];
    NSDictionary *attributes = [element attributes];
    NSString *content1 = [attributes objectForKey:@"content"];

    NSLog(@"save instagram image content1=%@",content1);

//获取.mp4链接,使用xPathQuery下面的

NSArray *elements = [cParser searchWithXPathQuery:@"//meta[@property='og:video']"];