blog/_posts/2019-05-27-wikipic.md
2022-01-04 20:42:55 +08:00

38 lines
1.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
layout: post
title: 使用PHP批量下载Mediawiki站点的图片
tags: [PHP, Mediawiki, 图片]
---
又是万能的PHP!不过还是Mediawiki API的功劳<!--more-->
最近我为了备份一下[某个Wiki站](http://zh.moegirl.org/)Ta们把R18名字空间的东西删的一干二净 ~~后来才知道原来转移到了[一个Wiki上](https://www.hmoegirl.com/),真的是好久没关注了~~ 然后学习了一下Mediawiki API来下载整个WikiTa们把站点导出也给弄没了QAQ
文本很好下载但是Mediawiki的图片我不知道存在哪里API文档翻烂了也没找到把图片解析成地址的API那怎么办呢
# 解决方案
“解析”emmmm……parse不错正好有这么一个action好的那就这样搞吧
# Code
```php
<?php
set_time_limit(0);
ignore_user_abort();
$list = array("图片数组");
$arrlength=count($list);
for($x=0;$x<$arrlength;$x++) {
$tmp = json_decode(file_get_contents("https://MediaWiki的地址/api.php?action=parse&text=[[File:".$list[$x]."]]&contentmodel=wikitext&formatversion=2&format=json"),true);
$preg='/src="(.*?)"/is';
preg_match($preg,$tmp[parse][text],$match);
$tt=$tt."
".$match[1];
}
$markout = fopen("List.txt", "w") or die("Unable to open file!");
fwrite($markout, $tt);
fclose($markout);
die("Finish");
?>
```
# P.S.
如果需要获取该Wiki的所有图片可以从`api.php?action=query&list=allimages`这里获取。