c - 使用 C 提取 Wiki 链接
问题描述
我需要编写一个程序来读取维基百科源文件并提取所有指向其他网页的链接。所有网页看起来都像示例:
<a href="/wiki/PageName" title="PageName">Chicken</a>
我基本上需要将 /wiki/ 之后的 PageName 与标题匹配,如果它们相同,如上所述,则只在终端上显示 PageName。
但是,不应匹配以下内容,因为它的格式与上述不同:(
<a href="http://chicken.com>Chicken</a>
这是指向维基百科外普通网站的链接)
<a href="/wiki/Chicken >Chicken</a
>(缺少 title= 部分)我试图实现的输出看起来像这样:
我已经为此工作了很长时间,并且能够做到以下几点:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE * file;
file = fopen(argv[1], "r");
char line[512];
char* search;
while(!feof(file)){
fgets(line,512,file);
search = strstr( line, "<a href=\"/wiki/");
if(search != NULL){
puts(search);
}
}
}
代码只过滤到 /wiki/ 但从这里开始我是空白的。我尝试了很多搜索,但无法获得线索。帮助将不胜感激。
解决方案
而不是while(!feof(file))
您可以使用while(fgets(line,512,file))
并通过添加几个验证,您的最终代码与预期输出将看起来像,
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif // MSC
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE * file;
if (argc != 2)
{
return -1;
}
file = fopen(argv[1], "r");
if (!file)
{
return -1;
}
char line[512];
char* search;
while (fgets(line, 512, file)) {
search = strstr(line, "<a href=\"/wiki/");
if (search != NULL) {
char *title = _strdup(search);
if (title)
{
char* start = strstr(title, ">");
char* end = strstr(start, "<");
if (end)
{
*end = 0;
}
if (strlen(start) >= 2)
{
puts(start + 1);
}
free(title);
title = 0;
}
}
}
fclose(file);
file = NULL;
return 0;
}
推荐阅读
- .htaccess - 如何使用 . .htaccess 文件?
- javascript - 动作创建者要么在渲染时调用,要么根本无法调用 React-Redux
- python - 何时使用“快捷方式”:列表推导、生成器表达式、lambda 函数?
- mongodb - 无法使用 mongoose 连接到 mongo 但可以在命令行上连接?
- reactjs - '$' 是在 IE 11 中运行的反应应用程序中的未定义错误
- function - 为什么我可以从 Go 中的类型调用 func?
- javascript - 每次单击按钮时打开顺序链接的脚本
- lisp - 如何在语法树 (CL) 中表示准引号
- phpmyadmin - 带有数据库的背包 Laravel 小部件
- django - Auth0 保存用户名而不是 'sub'