如何从信息框中获取维基百科上图像的链接?

发布于 2024-07-25 07:54:24 字数 170 浏览 6 评论 0原文

我正在解析维基百科信息框,我注意到一些信息框有图像字段 - 这些字段保存保存在维基百科某处的图像文件的名称。 然而,它们只包含文件名,而不是实际链接。

我检查了实时信息框中的图像链接,这些链接似乎不是来自一个来源,但来源各不相同。 考虑到我只有信息框条目中的图像名称,如何超链接到维基百科上的图像。

I'm parsing wikipedia infoboxes and I noticed that some infoboxes have image fields - these fields hold names of image files stashed on wikipedia somewhere. However they just contain the name of the file as is as opposed to the actual link.

I checked the links of the images on real live infoboxes and the links do not seem to be from one source but the sources vary. How can I hyperlink to an image on wikipedia considering I only have the name of the image from an infobox entry.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云柯 2024-08-01 07:54:24

根据 Wikipedia 文件路径中命名奇怪的组件,您需要运行md5 找出 url。 现在维基百科允许热链接,因此:

如果您有utf-8编码的$name,您需要执行以下操作:

$filename = replace($name, ' ', '_');
$digest = md5($filename);
$folder = $digest[0] . '/' . $digest[0] . $digest[1] . '/' .  urlencode($filename);
$url = 'http://upload.wikimedia.org/wikipedia/commons/' . $folder;

同样可以用于缩略图。

According to What are the strangely named components in Wikipedia file paths, you need to run md5 to find out url. Now wikipedia allows hotlinking, so:

If you have utf-8 encoded $name, you need to do the following:

$filename = replace($name, ' ', '_');
$digest = md5($filename);
$folder = $digest[0] . '/' . $digest[0] . $digest[1] . '/' .  urlencode($filename);
$url = 'http://upload.wikimedia.org/wikipedia/commons/' . $folder;

The same can be used for thumbnails.

冰之心 2024-08-01 07:54:24

现在有一种更简单的方法可以使用 Special:FilePath 页面热链接 Wikipedia 上的文件。 因此,如果您想链接英文维基百科上的文件Example.jpg,您可以使用https://en.wikipedia.org/wiki/Special:FilePath/Example.jpg

类似的链接应该适用于其他维基媒体基金会网站(例如维基共享资源)。

有关热链接的详细信息和建议可以在维基媒体的 Commons:Reusing_content_outside_Wikimedia 上找到。

There is now a simpler way to hotlink the files on Wikipedia using the Special:FilePath page. Hence, if you want to link the file Example.jpg on English Wikipedia, you can use https://en.wikipedia.org/wiki/Special:FilePath/Example.jpg.

Similar links should work for other Wikimedia Foundation sites (e. g. Wikimedia Commons).

Details and recommendations regarding hotlinking can be found on Wikimedia's Commons:Reusing_content_outside_Wikimedia.

何以心动 2024-08-01 07:54:24

下面是一个有效的 PHP 答案的 JavaScript 实现(归功于 Yuri Baburov):

var name = "filename.jpg";
var filename = name.replace(/ /g, "_"); 
var digest = md5(filename);
var folder = digest[0] + '/' + digest[0] + digest[1] + '/' + encodeURIComponent(filename);
var url = 'http://upload.wikimedia.org/wikipedia/commons/' + folder;

请注意,您必须包含外部 md5() 函数 (https://github.com/blueimp/JavaScript-MD5); 它不是 JS 原生的。

Here is a JavaScript implementation of a working PHP answer (credits to Yuri Baburov):

var name = "filename.jpg";
var filename = name.replace(/ /g, "_"); 
var digest = md5(filename);
var folder = digest[0] + '/' + digest[0] + digest[1] + '/' + encodeURIComponent(filename);
var url = 'http://upload.wikimedia.org/wikipedia/commons/' + folder;

Note that you must include external md5() function (https://github.com/blueimp/JavaScript-MD5); it is not native to JS.

我乃一代侩神 2024-08-01 07:54:24

您是否尝试过http://en.wikipedia.org/wiki/File:filename.jpg? 即使文件位于 Wikimedia Commons 上,上述 URL 仍应有效。

编辑:您是否尝试热链接图像? 如果是这样,维基百科将禁止热链接。 http://commons.wikimedia.org/wiki/Commons:Reusing_content_outside_Wikimedia#Hotlinking

2019 年 1 月 10 日更新:现在允许盗链< /a>:

盗链或 InstantCommons:可以直接使用文件
通过设置 MediaWiki wiki 在另一个网站内共享
即时共享,...

Have you tried http://en.wikipedia.org/wiki/File:filename.jpg ? Even if the files are located on Wikimedia Commons, the above URL should still work.

Edit: Are you trying to hotlink the image? If so, Wikipedia prohibits hotlinking. http://commons.wikimedia.org/wiki/Commons:Reusing_content_outside_Wikimedia#Hotlinking

Update 10-Jan-2019: Hotlinking is now permitted:

Hotlinking or InstantCommons: It is possible to use files directly on
Commons within another website, by setting up a MediaWiki wiki with
InstantCommons, ...

美煞众生 2024-08-01 07:54:24

我注意到 URL 前缀应该是:

http://upload.wikimedia.org/wikipedia/en/thumb/

此外,文件夹必须包含文件的重复名称,其大小指定为前缀:

http://upload.wikimedia.org/wikipedia/en/thumb/d/dd/Ruins-imperial-complex-milan-.jpg/220px-Ruins-imperial-complex-milan-.jpg

$filename = replace($name, ' ', '_');
$digest = md5($filename);
$urlencfile =  urlencode($filename)
$imgwidth = 220
$folder = $digest[0] . '/' . $digest[0] . $digest[1] . '/' . $urlencfile . '/' . $imgwidth . 'px-' . $urlencfile;
$url = 'http://upload.wikimedia.org/wikipedia/en/thumb/' . $folder;

I noticed that the URL prefix should be:

http://upload.wikimedia.org/wikipedia/en/thumb/

Moreover the folder must contain the repeated name of the file with a size specified as prefix:

http://upload.wikimedia.org/wikipedia/en/thumb/d/dd/Ruins-imperial-complex-milan-.jpg/220px-Ruins-imperial-complex-milan-.jpg

$filename = replace($name, ' ', '_');
$digest = md5($filename);
$urlencfile =  urlencode($filename)
$imgwidth = 220
$folder = $digest[0] . '/' . $digest[0] . $digest[1] . '/' . $urlencfile . '/' . $imgwidth . 'px-' . $urlencfile;
$url = 'http://upload.wikimedia.org/wikipedia/en/thumb/' . $folder;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文