You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
	
	
		
			
				
					
						
							|  |  |  |  | --- | 
					
						
							|  |  |  |  | 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来下载整个Wiki(Ta们把站点导出也给弄没了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`这里获取。 |