GDELT下的GKG数据自动下载
GDELT(www.gdeltproject.org)每时每刻监控着每个国家的几乎每个角落的100多种语言的新闻媒体–印刷的、广播的和web形式的,识别人员、位置、组织、数量、主题、数据源、情绪、报价、图片和每秒都在推动全球社会的事件,GDELT为全球提供了一个自由开放的计算平台。
GDELT主要包含两大数据集:Event Database(事件数据库)、Global Knowledge Graph (GKG,全球知识图谱),记录了从1969年至今的新闻,并于每十五分钟更新一次数据。
本文是可以从GDELT下载到的GKG数据链接自动下载所需数据内容的压缩包
用到的是VS2010
下载到的数据样本(取一份为例)
150383 297a16b493de7cf6ca809a7cc31d0b93 http://data.gdeltproject.org/gdeltv2/20150218230000.export.CSV.zip
318084 bb27f78ba45f69a17ea6ed7755e9f8ff http://data.gdeltproject.org/gdeltv2/20150218230000.mentions.CSV.zip
10768507 ea8dde0beb0ba98810a92db068c0ce99 http://data.gdeltproject.org/gdeltv2/20150218230000.gkg.csv.zip
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <windows.h>
#include <urlmon.h>
#include <fstream>
#include <string>
#include <fstream>
#include <sstream>
#pragma comment(lib, "urlmon.lib")
using namespace std;
BOOL FileExistsStatus(const CHAR* path)
{
DWORD dwAttribute = GetFileAttributes(path);
if (dwAttribute == 0XFFFFFFFF)
return false;//0XFFFFFFFF表示文件不存在
else return true;
}
BOOL DownloadFiles(const CHAR* url, const CHAR* downloadPath)
{
if (URLDownloadToFile(NULL, url, downloadPath, 0, 0) == S_OK && FileExistsStatus(downloadPath))
return true;
else return false;
}
int main()
{
//打开读取的文件
ifstream infile;
infile.open("D:\\PROgram\\DATE\\GDELTGKG\\data.txt", ios::in);
if (!infile.is_open())
{
cout << "读取文件失败" << endl;
return 0;
}
double num(0); //计数用
string buf; //读取文件中的每一行
string urltmp; //临时变量
string pathtmp; //临时变量
while (getline(infile,buf))
{
bool exists1 = buf.find("gdeltv2/201901") == string::npos; //约束时间
bool exists2 = buf.find(".gkg.csv.zip") == string::npos; //约束后缀
if (!exists1&&!exists2)
{
//获取正确的url
int lastspace = buf.find_last_of( " " );
int length = buf.length();
urltmp = buf.substr(lastspace+1,length-lastspace);
const char* url=urltmp.c_str();
cout<<url<<endl;
//获取存入的文件名
pathtmp = buf.substr(length-26,26);
string downloadpath = "D:\\PROgram\\DATE\\GDELTGKG\\";//下载到
downloadpath += pathtmp;
const char* downloadpath2=downloadpath.c_str();
cout<<downloadpath2<<endl;
DownloadFiles(url ,downloadpath2);//下载
num ++;
cout << num << endl;
}
}
cout<<"finish"<<endl;
infile.close();
system ("pause");
}
根据注释可以很方便修改代码