PHP 抓取豆瓣图书信息

<pre name="code" class="html"><!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />	
		<link rel="shortcut icon" href="images/themes/web.ico">
		<title>抓取结果</title>
		<link href="css/css1.css" rel="stylesheet" type="text/css" />
	</head>
	<body>
		<?php
			$str = $_POST['str'];//接收提交的表单搜索字符串
			if(!empty($str))//表单信息不为空
			{
				$bookAddress = array('https://api.douban.com/v2/book/search?q=',$str);//将固定网址和搜索字符串存入数组
				$book_href=implode("",$bookAddress);//合并字符串
				$handle = fopen($book_href,"rb");//打开文件(豆瓣会返回json)
				$content = "";
				while (!feof($handle)) {
					$content .= fread($handle, 100000);
				}
				fclose($handle);
				$content = json_decode($content);//JSON 格式的字符串进行解码
				// function get_file($url,$folder,$pic_name)//可以将json保存到本地
					// {  
						// set_time_limit(24*60*60); //限制最大的执行时间
						// $destination_folder=$folder?$folder.'/':''; //文件下载保存目录
						// $newfname=$destination_folder.$pic_name;//文件PATH
						// $file=fopen($url,'rb');

						// if($file)
						// {          
							// $newf=fopen($newfname,'wb');
							// if($newf)
							// {              
								// while(!feof($file))
								// {                    
									// fwrite($newf,fread($file,1024*8),1024*8);
								// }
							// }
							// if($file)
							// {              
								// fclose($file);
							// }
							// if($newf)
							// {              
								// fclose($newf);
							// }
						// }       
					// }  
				foreach($content->books as $book)//遍历json
				{ 
					// get_file($book->image,"images/book",''.$book->isbn13.'.jpg');//保存图片到本地
					/* echo '<div class="jiegou"><img src="images/book/'.$book->isbn13.'.jpg"><br>'.$book->title.'<br>'.$book->publisher.'</div>'; */
					$bookAddress = array('http://book.douban.com/subject/',$book->id);
					$book_href=implode("",$bookAddress);//合并字符串
					echo '<div class="jiegou"><a href="'.$book_href.'"><img src="'.$book->image.'"></a><br>'.$book->title.'<br>'.$book->publisher.'</div>';//链接到豆瓣图书信息页
					
				}
			}
			else
				echo '<script language="JavaScript">;alert("输入为空,请重新输入");history.go(-1)</script>';

		?>
	</body>
<html>



                
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值