English 中文(简体)
只从目标C 中的 <div> 获取文本和图像
原标题:Get only text and images from <div> in Objective-C
  • 时间:2012-05-27 14:34:34
  •  标签:
  • ios
  • html
  • rss

I m making a news reading application. The best site I found was http://fulltextrssfeed.com/ It takes the text and images from any webpage and gives back clean text. As they don t have an API I need some way to get the data from the <div>. This is the div ID:

<div id="preview">

我怎样才能将 leach 上传到种子上, 只获取其内容( 如果没有 HTML 标签, 如果有我可以在周围工作的话, 将会是附加的 ) 。

最佳回答

我不确定您的问题, 但如果您在使用 obj- c, 我建议您使用 < a href="https://github.com/topfunky/hpple" rel = "nofollow" >Hpple 。 这是一个非常好的 XML/ HTML 解析器 。

要使用它, 您需要将 ${SDKROOT}/ usr/ including/ libxml2 添加到“ 头人搜索路径 ” 中 < em> 您的项目选项 中, 并在“ 其他链接旗” 中添加 < code>- lxml2

当您已经拥有 Hpple 文件时, 请将其拖动到您的代码中 : TFHpple.h , TFHpple.m , TFHppleEplement.h , TFHppleEple.m , XPathQuery.h , XPathQuery.m

在代码(获取您的 div “ preview ” ) 中添加:

NSData *htmlData = [[NSString stringWithContentsOfURL:[NSURL URLWithString: @"http://www.yoursite.com/index.html"]] dataUsingEncoding:NSUTF8StringEncoding];

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

[xpathParser release];
[htmlData release];

现在我们有了带有 Hpple 的“ 预览 div ” 。 要获得某些子类( 如 < code> p 或 < code> a ), 请使用它 :

NSArray *elements  = [xpathParser searchWithXPathQuery:@"//div[@id= preview ]/p/text()"]; 

更多信息,请查看XPath 语法 。请查看 辅导

希望能帮上忙

问题回答

我用这个来剥光所有 html非常有帮助

< a href=" "https://gist.github.com/1202238" rel="no follow" >NSString +strade HTML





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!