我试图从微软学术图表中检索0级FieldOfStudy“计算机科学”的全部“研究领域”。到目前为止,我通常有以下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-Subscription-Key: my_subscription_key' \
-d 'expr=Ty%3D'\''6'\''&attributes=Id%2CFL%2CFN%2CFC.FN%2CFP.FN%2CFC.FId%2CFP.FId'这不会引发任何错误,但需要进一步修改,以便检索:
虽然我是以curl的方式这样做的,但我也愿意使用python方法,以防这是一个更好的选择。
发布于 2020-01-21 17:41:59
如果您的目标是枚举计算机科学项下的所有后代研究领域,则需要进行递归调用,因为每个学习领域(意味着父母和子女,而不是祖父母或孙辈)只对直接级别进行索引。
幸运的是,使用查询表达式"Composite(FP.FId=parent_fos_id)“来完成这一任务非常简单。
下面是一些示例C#代码,用于获取所有的后代研究领域(抱歉,我不精通Python,但应该很容易弄清楚我在做什么):
static void GetAllDescendantFieldsOfStudy(long fieldOfStudyId, int level, ref SortedSet<long> descendants)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "_subscription_key_");
var jsonString =
client
.GetStringAsync(
new 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"))
.Result;
var jsonObject = Newtonsoft.Json.Linq.JObject.Parse(jsonString);
var childCount = jsonObject["entities"].Count();
if (childCount > 0)
{
var children = jsonObject["entities"];
foreach (var child in children)
{
var childId = child.Value<long>("Id");
if (!descendants.Contains(childId))
{
descendants.Add(childId);
Console.WriteLine($"{new String('\t', level)}Expanding {child.Value<string>("DFN")}");
GetAllDescendantFieldsOfStudy(childId, level + 1, ref descendants);
}
}
}
}要使用它,只需使用计算机科学ID调用它,即:
var descendants = new SortedSet<long>();
GetAllDescendantFieldsOfStudy(41008148, 0, ref descendants);不幸的是,没有办法绕过1000的最大结果数。您只需使用偏移量来拆分请求。
希望这能有所帮助!
https://stackoverflow.com/questions/59844086
复制相似问题