HTML 图像字符串解析器

发布于 2025-01-07 04:09:38 字数 306 浏览 0 评论 0原文

我想知道是否有人对我的问题有任何想法。我需要从 UIWebView 加载的 html 文件中提取所有图像文件。我已将文件加载到 NSString 中,现在需要解析该文件。我已经使用 ComponentsSeparatedByString 创建了一个数组,搜索 .jpg、.gif 等。然后尝试向后移动以到达文件的开头。我最好的解决方案是能够将 html 解析为包含 img src="source" width="" height="" 等的 NSArray

任何帮助或提示将不胜感激。我最后的努力是对整个文件进行从左到右的搜索/替换,以找到我需要的字符串,但希望有一种更快的方法。

I was wondering if anyone had any ideas for my problem. I need to extract all the image files from a html file loaded by a UIWebView. I have the file loaded into a NSString and now need to parse the file. I have gone through creating an array with componentsSeparatedByString searching for .jpg, .gif, etc. Then trying to work backwards to get to the beginning of the file. My best solution would be to be able to parse out the html into an NSArray containing img src="source" width="" height="" etc

Any help or hints would be appreciated. My last ditch effort would be doing a search/replace left to right of the entire file to find the strings I need, but hope there is a quicker way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁人与我共长歌 2025-01-14 04:09:38

不解析 HTML,使用 libxml2。它具有广泛的面向 HTML 的解析/遍历功能,使您可以通过元素以编程方式导航文档。

我还没有面向 HTML 的示例代码可供使用,但这应该只是 htmlReadDoc() 获取解析后的文档;然后根据读取树示例调整您的遍历。

void print_element_names(xmlNode * a_node)
{
    xmlNode *cur_node = NULL;

    for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
        if (cur_node->type == XML_ELEMENT_NODE) {
            printf("node type: Element, name: %s\n", cur_node->name);
        }

        print_element_names(cur_node->children);
    }
}

// ... call your version of this function with the root node of the document

Don't parse HTML, use libxml2. It has an extensive range of HTML-oriented parsing/traversing functions that let you navigate your document programmatically by elements.

I haven't got HTML-oriented sample code to work from, but it should just be a matter of htmlReadDoc() to get a parsed document; and then adapt your traversal from the read tree example.

void print_element_names(xmlNode * a_node)
{
    xmlNode *cur_node = NULL;

    for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
        if (cur_node->type == XML_ELEMENT_NODE) {
            printf("node type: Element, name: %s\n", cur_node->name);
        }

        print_element_names(cur_node->children);
    }
}

// ... call your version of this function with the root node of the document
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文