php代碼擷取豆瓣網上電影資訊的簡介

來源:互聯網
上載者:User

[PHP]代碼

  1. //搜尋連結
  2. function search_link($moviename)
  3. {
  4. //構造url,其中max-results可根據需要更改
  5. $urlString = 'http://api.douban.com/movie/subjects?q='.$moviename.'&start-index=1&max-results=1&alt=json';
  6. //print_r($urlString);
  7. $urlString=mb_convert_encoding($urlString, "UTF-8", "GBK");//將Url轉換為utf-8編碼
  8. $r = new HttpRequest($urlString,HttpRequest::METH_GET);//請求
  9. $response = $r->send();
  10. $result = $r->getResponseBody();
  11. $obj = json_decode($result);//解析成json格式
  12. if($entry = @$obj->{'entry'}){
  13. //搜尋連結並存在數組中返回
  14. for($i = 0;$i $link=$entry[$i]->{'link'};
  15. for($j = 0;$j $arr = (array)$link[$j];
  16. $key = array_search("self",$arr);
  17. if($key){ //判斷key是否存在
  18. $str = $arr["@href"].'?alt=json'; //構造返回連結的格式
  19. break;
  20. }
  21. }
  22. $link_array[] = $str;
  23. }
  24. return $link_array;//返回連結
  25. } else
  26. echo"Not Found!";
  27. }
  28. //查詢連結並將各變數放入$GLOBAL 數組
  29. function get_info($urlString)
  30. {
  31. $r = new HttpRequest($urlString,HttpRequest::METH_GET);
  32. $response = $r->send();
  33. $result = $r->getResponseBody();
  34. //if..else.. 判斷開啟連結是否為空白
  35. if ($obj = json_decode($result)){
  36. //將影片的資訊放在全域數組中
  37. $title = $obj->{'title'};
  38. $author = $obj->{'author'};
  39. $summary = $obj->{'summary'};
  40. $ID=$obj->{'id'};
  41. $link = $obj->{'link'};
  42. $gd = (array)$obj->{'gd:rating'};
  43. $db_array = array();
  44. $db = $obj->{'db:attribute'};
  45. //將db:atribute中的值放進數組
  46. foreach ($db as $value){
  47. $value_array = (array)($value);
  48. $v = $value_array["@name"];
  49. $k = $value_array["\$t"];
  50. if (array_key_exists("@lang",$value_array)){
  51. $lang=$value_array["@lang"];
  52. $k=$k.'['.$lang.']';
  53. }
  54. $db_array[$v][]=$k;
  55. }
  56. //設定對應數組的索引值
  57. @$db_array_key = array('【影片原名】','【別 名】','【導 演】','【編 劇】','【官方網站】','【IMDB連結】','【出品年代】',
  58. '【國 家】','【電影類型】','【上映日期】','【放映長度】','【集 數】','【語 言】','【演 員】');
  59. @$db_array_value = array($db_array["title"],$db_array["aka"],$db_array["director"],$db_array["writer"],$db_array["website"],
  60. $db_array["imdb"],$db_array["year"],$db_array["country"],$db_array["movie_type"],$db_array["pubdate"],
  61. $db_array["movie_duration"],$db_array["episodes"],$db_array["language"],$db_array["cast"]);
  62. @$db_array = array_combine($db_array_key,$db_array_value);
  63. } else{
  64. echo "Empty link!";
  65. }
  66. $info=array("title"=>$title,"author"=>$author,"summary"=>$summary,"ID"=>$ID,"link"=>$link,"gd"=>$gd,"db_array"=>$db_array);
  67. return $info;
  68. }
  69. //將影片資訊寫入檔案
  70. function write_info($fi,$info)
  71. {
  72. //fwrite($fi, "******");寫入檔案
  73. fwrite($fi, "【影片原名】");
  74. foreach ($info["title"] as $k=>$v)
  75. fwrite($fi,"$v\r\n");
  76. fwrite($fi,"\r\n");
  77. fwrite($fi,"【出 品 人】");
  78. foreach ($info["author"] as $value)
  79. foreach ($value as $key)
  80. foreach ($key as $v) {
  81. fwrite($fi,"$v\r\n");
  82. }
  83. fwrite($fi,"\r\n");
  84. //db:attribute
  85. foreach ($info["db_array"] as $key=>$value){
  86. if(count($value)){
  87. fwrite($fi,$key);
  88. if($value==$info["db_array"]['【演 員】']){
  89. foreach($value as $k=>$v)
  90. fwrite($fi,"$v\r\n ");
  91. } else{
  92. foreach($value as $k=>$v)
  93. fwrite($fi,"$v ");
  94. fwrite($fi,"\r\n");
  95. }
  96. }
  97. }
  98. fwrite($fi,"\r\n");
  99. //影片簡介
  100. fwrite($fi, "【簡 介】\r\n");
  101. foreach ($info["summary"] as $k=>$v){
  102. fwrite($fi," $v \r\n");
  103. }
  104. fwrite($fi,"\r\n");
  105. //ID
  106. fwrite($fi,"【I D】");
  107. foreach ($info["ID"] as $k=>$v){
  108. fwrite($fi,$v);
  109. }
  110. fwrite($fi,"\r\n");
  111. //連結
  112. fwrite($fi,"【鏈 接】\r\n");
  113. foreach ($info["link"] as $value){
  114. foreach ($value as $k=>$v){
  115. fwrite($fi, " $v ");
  116. }
  117. fwrite($fi,"\r\n");
  118. }
  119. fwrite($fi,"\r\n");
  120. //評論
  121. fwrite($fi,'【評 論】');
  122. fwrite($fi,$info["gd"]["@numRaters"]);
  123. fwrite($fi,"\r\n");
  124. fwrite($fi,'【最高】'); fwrite($fi,$info["gd"]["@max"]);
  125. fwrite($fi," 【最低】");fwrite($fi,$info["gd"]["@min"]);
  126. fwrite($fi,' 【平均】');fwrite($fi, $info["gd"]["@average"]);
  127. fwrite($fi,"\r\n");
  128. }
  129. //在指定的檔案夾中尋找電影並返回電影名
  130. function find_dir($dir)
  131. {
  132. if(is_dir($dir)){ //判斷所給路徑名是否為目錄
  133. if($fdir=opendir($dir)){ //開啟目錄
  134. while(false!==($file=readdir($fdir))){ //遍曆目錄中的檔案
  135. if ($file != "." && $file != "..")
  136. $files[]=$file; //將尋找到的檔案放入數組並返回
  137. }
  138. closedir($fdir);
  139. print_r($file);
  140. return $files;
  141. }
  142. }
  143. }
  144. //將產生的txt檔案放在指定的目錄下
  145. function write_to_dir($dir,$moviename)
  146. {
  147. if (is_dir($dir)){ //判斷所給路徑名是否為目錄
  148. if($fdir=opendir($dir)){ //開啟目錄
  149. while(false!==($file=readdir($fdir))){ //遍曆目錄中的檔案
  150. if ($file != '.' && $file != '..') {
  151. $dirname=$dir.'\\'.$file;
  152. if (is_dir($dirname)&&$file==$moviename) { //尋找指定的檔案夾
  153. $filename=$dirname.'\\'.$moviename.'.txt';
  154. //if(!file_exists($filename)){
  155. if($fi=fopen($filename,'w+')){ //開啟指定檔案,若不存在,建立檔案並返回
  156. closedir($fdir);
  157. return $fi;
  158. }else
  159. echo"Fail to Open file!";
  160. //}
  161. } else
  162. echo "Dir Not Exists!";
  163. }
  164. }
  165. } else {
  166. echo 'Fail to Open!';
  167. return;
  168. }
  169. } else {
  170. echo 'Directory Not Exists!';
  171. return;
  172. }
  173. }
  174. $dir = 'E:\film';
  175. if($movienames = @find_dir($dir)){ //在指定目錄中獲得電影名,進行搜尋
  176. foreach ($movienames as $key=>$moviename){
  177. if($link_info = search_link($moviename)){
  178. //搜尋影片名
  179. if ($fi=write_to_dir($dir,$moviename)){ //寫入指定檔案夾
  180. fwrite($fi,"\r\n");
  181. fwrite($fi,'');
  182. fwrite($fi,"\r\n\r\n\r\n".'※※※※※※※※※※※※※※※※※※※※※ 影片資訊 ※※※※※※※※※※※※※※※※※※※※※'."\r\n");
  183. foreach ($link_info as $k){
  184. $info=get_info($k); //擷取連結資訊
  185. write_info($fi,$info); //在txt檔案中寫入資訊
  186. fwrite($fi,"*************************************************************************************************\r\n");
  187. }
  188. fclose($fi);//寫入完成,關閉檔案
  189. }
  190. } else
  191. echo "Empty!";
  192. }
  193. } else
  194. echo "Movies Not Exists!";
  195. ?>
複製代碼
網上電影, php
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.