从Wikipedia API提取数据

编程入门 行业动态 更新时间:2024-10-25 16:24:21
本文介绍了从Wikipedia API提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望能够使用json从Wikipedia中提取标题和描述.所以...维基百科不是我的问题,我是json的新手,并且想知道如何使用它.现在我知道有成百上千的教程,但是我已经工作了几个小时,它什么也没显示,这是我的代码:

I would like to be able to extract a title and description from Wikipedia using json. So... wikipedia isn't my problem, I'm new to json and would like to know how to use it. Now I know there are hundreds of tutorials, but I've been working for hours and it just doesn't display anything, heres my code:

<?php $url="en.wikipedia/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url"; $json = file_get_contents($url); $data = json_decode($json, TRUE); $pageid = $data->query->pageids; echo $data->query->pages->$pageid->title; ?>

只需单击它即可更轻松:

Just so it easier to click:

  • zh.wikipedia/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects& inprop = url& indexpageids

我知道我可能做错了一点小事,但是它确实困扰着我,还有代码……我习惯于使用xml,并且我已经做了很多切换,所以您可以解释一下这对我和未来的访问者来说有点麻烦,因为我很困惑……您需要我没说的任何东西,只需评论一下,确定可以得到,谢谢,谢谢!

I know I've probably just done a tiny thing wrong, but its really bugging me, and the code... I'm used to using xml, and I have pretty much just made the switch, so can you explain it a bit for me and for future visitors, because I'm very confused... Anything you need that I haven't said, just comment it, im sure I can get it, and thanks, in advance!

推荐答案

$pageid返回带有一个元素的数组.如果您只想握拳,那您应该这样做:

$pageid was returning an array with one element. If you only want to get the fist one, you should do this:

$pageid = $data->query->pageids[0];

您可能会收到以下警告:

You were probably getting this warning:

Array to string conversion

完整代码:

$url = 'en.wikipedia/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url&indexpageids'; $json = file_get_contents($url); $data = json_decode($json); $pageid = $data->query->pageids[0]; echo $data->query->pages->$pageid->title;

更多推荐

从Wikipedia API提取数据

本文发布于:2023-11-24 19:48:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626578.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   Wikipedia   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!