Javascript 如何使用ajax在wikipedia中获取特定信息

Javascript 如何使用ajax在wikipedia中获取特定信息,javascript,json,ajax,api,wikipedia,Javascript,Json,Ajax,Api,Wikipedia,如何获得在绝大多数搜索中始终显示在右框中的信息(使用AJAX在图像中显示的信息)?我已经使用了API,但是我没有找到我想要的信息 好吧,天哪,我从来没有花这么多时间回答关于stackoverflow的问题 下面有一个工作片段,虽然很脏,但仍在工作: //url中的维基百科文章 const wiki_article_title='Grand_teft_Auto_V'; //请查收https://www.mediawiki.org/wiki/API:Get_the_contents_of_a_pa

如何获得在绝大多数搜索中始终显示在右框中的信息(使用AJAX在图像中显示的信息)?我已经使用了API,但是我没有找到我想要的信息


好吧,天哪,我从来没有花这么多时间回答关于stackoverflow的问题

下面有一个工作片段,虽然很脏,但仍在工作:

//url中的维基百科文章 const wiki_article_title='Grand_teft_Auto_V'; //请查收https://www.mediawiki.org/wiki/API:Get_the_contents_of_a_page 常量url\u api=`https://en.wikipedia.org/w/api.php?action=parse&page=${wiki\u article\u title}&prop=text&formatversion=2&origin=*`; 函数extractInfoboxFromWikidoc{ //这里我们提取api提供的json const json=doc.querySelector'pre'; const obj=JSON.parsejson.innerText; 让html=obj.parse.text; //无论出于何种原因,html文本中都存在“\n”子字符串 //因此,我们使用正则表达式删除它们,以避免破坏“JSON.parse” html=html.replace/\\n/gm; //获取api响应的有趣部分 const node=document.createElement'div'; node.innerHTML=html; const infobox=node.querySelector'.infobox'; 让infos=[…infobox.firstChild.children]; 让输出={}; //解析标题 输出['title']=infos[0]。查询选择器'th'。innerText; infos.shift; //解析图像url 输出['image_url']=infos[0]。查询选择器'a'; infos.shift; //遍历节点以使用值映射标题 infos.forEach tr=>{ const key=tr.querySelector'th'。innerText; iftr.querySelector'ul'{ const lis=tr.queryselectoral'li'; 常量值=[…lis]。映射li=>li.innerText; 输出[键]=值; }否则{ const value=tr.querySelector'td'.innerText; 输出[键]=值; } }; //返回美化json 返回JSON.stringifyoutput,null,4; } fetchurl\u api .thenresponse=>response.text .thentext=>{ const parser=新的DOMParser; const doc=parser.parseFromStringtext,“text/html”; const WHAT_YOU_所需=extractInfoboxFromWikidoc; 常数格式化=``; 书面形式的文件; };