gpt4 book ai didi

c - 如何在 html 中搜索字符串模式,用 C 编码?

转载 作者:行者123 更新时间:2023-11-30 15:27:15 25 4
gpt4 key购买 nike

我需要在 html 文件中搜索标题(字符串)。为此,我执行了 strstr 来获取标签“li”,其中包含标签“title=\”,其中包含我想要的字符串。

例如:使用下面的数组,我需要获取书名、标题内的内容。但是,我需要 html 正文中的所有标题,其中有数百个。

<li><i><a href="/wiki/Animal_Farm" title="Animal Farm">A Revolução dos Bichos</a></i> (<a href="/wiki/1945" title="1945">1945</a>), de <a href="/wiki/George_Orwell" title="George Orwell">George Orwell</a>.</li>

我试图使用 strlen 运行“for”来获取其循环条件(行长度)。在这个 for 中,我使用 strstr 来获取 title=”,最终复制字符串直到引号末尾。

像这样:

for (i=0, i<len, i++){
if(strstr(array[i] == " title=\""){
do{
temp[i] = array[i];
}while((strcmp(array[i], "\""));
}
}

这就是我挣扎的地方。如何使用模式(条件)获取字符串内部的字符串?有什么建议吗?

提前谢谢您!问候。

最佳答案

“以正确的方式”解析 HTML 比一次检查一个字符串要复杂得多。我下面的代码比相反的方式做了更多正确的事情 - 但这部分是由于缺乏信息。

您的 HTML 格式正确吗?可以title属性包含字符串 lititle ,或流浪<>人物?是否需要考虑标签内部可能出现空格,如< li > ?所有属性都是用双引号写的" ,或者可以有单引号 '还有吗?

我的代码显示了 HTML 解析的一般思想:从一个 < 跳转转到下一个并检查其后面的 HTML 命令。但正如您所看到的,它非常丑陋,而且虽然它“完成了工作”,但几乎无法维护。

对于在明确定义的参数内快速完成工作,它可能会做;对于所有其他人,请寻找通用的 HTML 解析库,它将使您免受上述警告的影响,并为元素和属性提供用户友好的界面。

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
char str[] = "<li><i><a href=\"/wiki/Animal_Farm\" title=\"Animal Farm\">A Revolução dos Bichos</a></i> (<a href=\"/wiki/1945\" title=\"1945\">1945</a>), de <a href=\"/wiki/George_Orwell\" title=\"George Orwell\">George Orwell</a>.</li>"
"<li><i><a href=\"/wiki/Animal_Farm_II\" title=\"Animal Farm II: Return of the Hog\">A Revolução dos Bichos</a></i> (<a href=\"/wiki/1945\" title=\"1945\">1945</a>), de <a href=\"/wiki/George_Orwell\" title=\"George Orwell\">George Orwell</a>.</li>";
char *html_walker;
html_walker = str;
do
{
html_walker = strstr(html_walker, "<");
if (!html_walker)
break;
/* Is this "LI"? */
if (!strncasecmp(html_walker+1, "LI", 2) &&
!isalnum(html_walker[3]))
{
/* Yes. Scan following HTML entries for 'title' until we find an "</LI>" */
do
{
/* an "</LI>" code. Bye. */
if (*html_walker == '<')
{
html_walker++;
if (!strncasecmp(html_walker+1, "/LI", 3) &&
!isalnum(html_walker[4]))
{
while (*html_walker && *html_walker != '>')
html_walker++;
if (*html_walker == '>')
html_walker++;
break;
}
/* Not an "</LI>" code. Look for 'title' */
while (*html_walker && *html_walker != '>')
{
if (isspace (*html_walker) &&
!strncasecmp(html_walker+1, "TITLE=\"", 7))
{
html_walker += 8;
printf ("title [");
while (*html_walker && *html_walker != '"')
{
printf ("%c", *html_walker);
html_walker++;
}
printf ("]\n"); fflush (stdout);
/* We found a title, so skip to next </LI> */
do
{
html_walker = strstr(html_walker, "<");
if (!html_walker)
break;
/* Is this "/LI"? */
if (!strncasecmp(html_walker+1, "/LI", 3) &&
!isalnum(html_walker[4]))
break;
html_walker++;
} while (html_walker && *html_walker);
break;
}
html_walker++;
}
if (*html_walker == '>')
html_walker++;
} else
{
html_walker++;
}
} while (*html_walker);
} else
{
/* Skip forward to next '<' */
html_walker++;
}
} while (html_walker && *html_walker);
return 0;
}

关于c - 如何在 html 中搜索字符串模式,用 C 编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27107700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com