Curl 使用Microsoft Academic Graph API检索研究领域
我试图从微软学术图表中检索0级“计算机科学”研究领域的全部“研究领域”。到目前为止,我有以下用于检索字段的curl代码:Curl 使用Microsoft Academic Graph API检索研究领域,curl,microsoft-cognitive,academic-graph,Curl,Microsoft Cognitive,Academic Graph,我试图从微软学术图表中检索0级“计算机科学”研究领域的全部“研究领域”。到目前为止,我有以下用于检索字段的curl代码: curl -X POST \ https://api.labs.cognitive.microsoft.com/academic/v1.0/evaluate \ -H 'Cache-Control: no-cache' \ -H 'Content-Type: application/x-www-form-urlencoded' \ -H 'Ocp-Apim-S
curl -X POST \
https://api.labs.cognitive.microsoft.com/academic/v1.0/evaluate \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Ocp-Apim-Subscription-Key: my_subscription_key' \
-d 'expr=Ty%3D'\''6'\''&attributes=Id%2CFL%2CFN%2CFC.FN%2CFP.FN%2CFC.FId%2CFP.FId'
这不会引发任何错误,但需要进一步修改,以便检索:
虽然我是在curl中这样做的,但如果python方法是一个更好的选择,我也会对它持开放态度。如果您的目标是枚举计算机科学下的所有后代研究领域,那么您需要进行递归调用,因为每个研究领域只有直接级别被索引(指父母和子女,而不是祖父母或孙子孙女) 幸运的是,使用查询表达式“Composite(FP.FId=parent\u fos\u id)”来完成这一任务相当简单 下面是一些示例C#代码,用于获取所有后代研究领域(抱歉,我不懂Python,但应该很容易理解我在做什么):
静态void GetAllDescendantFieldsOfStudy(长字段的StudyId、int级别、ref-SortedSet子体)
{
var client=新的HttpClient();
client.DefaultRequestHeaders.Add(“Ocp Apim订阅密钥”,“订阅密钥”);
var jsonString=
客户
.GetStringAsync(
新Uri($”https://api.labs.cognitive.microsoft.com/academic/v1.0/evaluate?expr=Composite(FP.FId={fieldOfStudyId})&model=latest&count=1000&offset=0&attributes=Id,DFN“)
.结果;
var jsonObject=Newtonsoft.Json.Linq.JObject.Parse(jsonString);
var childCount=jsonObject[“实体”].Count();
如果(childCount>0)
{
var children=jsonObject[“实体”];
foreach(儿童中的儿童变量)
{
var childId=child.Value(“Id”);
如果(!subjects.Contains(childId))
{
添加(childId);
Console.WriteLine($“{newstring('\t',level)}扩展{child.Value(“DFN”)});
GetAllDescendantFieldsOfStudy(childId,级别+1,参考后代);
}
}
}
}
要使用它,只需使用计算机科学ID,即:
var descendants = new SortedSet<long>();
GetAllDescendantFieldsOfStudy(41008148, 0, ref descendants);
var后代=新的SortedSet();
GetAllDescendantFieldsOfStudy(41008148,0,参考后代);
不幸的是,无法绕过1000的最大结果计数。您只需使用偏移量来分解请求
希望这有帮助
var descendants = new SortedSet<long>();
GetAllDescendantFieldsOfStudy(41008148, 0, ref descendants);