从 HTML 中解析出 URL 的信息C语言中的标签

发布于 2024-10-06 08:34:34 字数 394 浏览 0 评论 0原文

我的应用程序获取一个包含大量链接的大型 html 格式文件作为其数据的一部分。就像您在 Google 或 Yahoo 或其他搜索引擎上搜索任何内容时会得到的内容一样:URL 列表以及描述或其他文本。

我一直在尝试提供一个可以解析 URL 和描述并将它们保存到文本文件中的函数,但事实证明这很困难,至少对我来说是这样。因此,如果我有:

访问 W3Schools

我会解析 http://www.w3schools .com访问 W3Schools 并将它们保存在文件中。

有什么办法可以实现这个目标吗?用普通的 C 语言?
任何帮助表示赞赏。

My application gets as part of its data a large html formatted file that contains large amounts of links. Something like what you would get if you search anything on Google or Yahoo or other search engines: a list of URLs and the description or other text.

I've been trying to come out with a function that can parse the URL and the description and save them into a text file but it's proven hard, at least to me. So, if I have:

<a href="http://www.w3schools.com">Visit W3Schools</a>

I would parse http://www.w3schools.com and Visit W3Schools and save them in a file.

Any way to achieve this? in plain C?
Any help is appreciated.

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

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

发布评论

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

评论(1

梓梦 2024-10-13 08:34:34

您确实需要一个合适的 html 解析器,但对于一些快速而肮脏的东西,请尝试:

bool get_url(char **data, char **url, char **desc)
{
  bool result = false;
  char *ptr = strstr(*data, "<a");

  if(NULL != ptr)
  {
    *data = ptr + 2;

    ptr = strstr(*data, "href=\"");
    if(NULL != ptr)
    {
      *data = ptr + 6;
      *url = *data;

      ptr = strchr(*data, '"');
      if(NULL != ptr)
      {
        *ptr = '\0';
        *data = ptr + 1;

        ptr = strchr(*data, '>');
        if(NULL != ptr)
        {
          *data = ptr + 1;
          *desc = *data;

          ptr = strstr(*data, "</a>");
          if(NULL != ptr)
          {
            *ptr = '\0';
            *data = ptr + 4;
            result = true;
          }
        }
      }
    }
  }

  return result;
}

不是 data 被更新为超出解析的数据(它是一个输入输出参数)并且传入的字符串得到修改的。我感觉很懒/太忙,无法使用内存分配的返回字符串来完成完整的解决方案。

另外,您可能应该在紧密范围大括号(第一个除外)的级联上返回错误,这就是我这样将它们堆叠起来的部分原因。还有其他更简洁的解决方案可以调整为更通用。

所以基本上你会重复调用该函数,直到它返回 false。

You really need a proper html parser, but for something quick and dirty, try:

bool get_url(char **data, char **url, char **desc)
{
  bool result = false;
  char *ptr = strstr(*data, "<a");

  if(NULL != ptr)
  {
    *data = ptr + 2;

    ptr = strstr(*data, "href=\"");
    if(NULL != ptr)
    {
      *data = ptr + 6;
      *url = *data;

      ptr = strchr(*data, '"');
      if(NULL != ptr)
      {
        *ptr = '\0';
        *data = ptr + 1;

        ptr = strchr(*data, '>');
        if(NULL != ptr)
        {
          *data = ptr + 1;
          *desc = *data;

          ptr = strstr(*data, "</a>");
          if(NULL != ptr)
          {
            *ptr = '\0';
            *data = ptr + 4;
            result = true;
          }
        }
      }
    }
  }

  return result;
}

Not that data gets updated to be beyond the data parsed (it's an in-out parameter) and that the string passed in gets modified. I'm feeling lazy/too busy to do full solutions with memory allocated return strings.

Also you probably ought to return errors on the cascade of close scope braces (except the first one) which is partly why I stacked them up like that. There are other neater solutions that can be adapted to be more generic.

So basically you then call the function repeatedly until it returns false.

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