多种方式通过ISBN获取图书信息

前言:在做图书查询,二手书买卖等经常需要用到图书信息,我们可以采用一种便捷的方式就是扫描图书后面的条形码,查询获取图书的信息。
方式一:网页抓包法
目前很多人都是采用douban的接口,但是现在豆瓣关闭的图书信息查询的接口,但是我们还是一个采用网页爬取的技术抓取信息。爬虫的方式很多中,go、php、python、java、js、.net等都是可以的

例如我写的一个tp5爬取接口

tips:可以将获取到图书信息存到数据库中,下次再次请求先查一遍数据库,如果有则不用爬虫,这样相对来说可以减轻压力,久而久之你也有很多图书的数据了。

  public function getBookInfo()
    {
        $isbn = $_GET['isbn'];
        try {
            $surl = 'https://book.douban.com/isbn/' . $isbn . '/';
            $headers = json_encode(get_headers($surl), true);
            $headers = json_encode($headers, true);
            $surl = $this->cut($headers, 'Location: ', '"');
            $surl = str_replace('\\', '', $surl);//302地址
            $data = $this->getIsbn($surl);
            $data_1 = $this->cut($data, 'application/ld+json">', '</script>');
            $data_1 = json_decode($data_1, true);
            $res['title'] = $data_1['name'];//书名

            $res['logo'] = $this->cut($data, 'data-pic="', '"');//图标

            $author = $data_1['author'];
            if (!isset($author[0]) || $author[0] == '') {
                $author[0]['name'] = '未知';
            }
            $res['author'] = $author;//作者
            //相关书籍推荐
            $publisher = $this->cut($data, '出版社:</span>', '<br/>');
            if ($publisher == '') {
                $publisher = '未知';
            }
            $res['publisher'] = $publisher;//出版社

            $author_desc = $this->cut($data, 'class="indent ">', '</div>');
            $res['author_desc'] = $this->cut($author_desc, '<p>', '</p>');
            if ($res['author_desc'] == "") {
                $res['author_desc'] = '未知';
            }
            $res['author_desc'] = $author_desc;//作者简介
            $published = $this->cut($data, '出版年:</span>', '<br/>');
            if ($published == '') {
                $published = '未知';
            }
            $res['published'] = $published;//出版年

            $page = $this->cut($data, '页数:</span>', '<br/>');
            if ($page == '') {
                $page = '未知';
            }
            $res['page'] = $page;//页数

            $price = $this->cut($data, '定价:</span>', '<br/>');
            if ($price == '') {
                $price = '未知';
            }
            $res['price'] = $price;//定价
            $designed = $this->cut($data, '装帧:</span>', '<br/>');
            if ($designed == '') {
                $designed = '未知';
            }
            $res['designed'] = $designed;//装帧

            $description = $this->cut($data, 'class="intro">', '</p>');
            if ($description == '') {
                $description = '未进行描述';
            } else {
                $description = explode('<p>', $description)[1];
            }
            $res['description'] = $description;//简介
            return_msg(200, '请求成功', $res);
        } catch (Exception $e) {
            return_msg(500, '服务器内部错误', $e);
        }


    }

    private function cut($content, $start, $end)
    {
        $r = explode($start, $content);
        if (isset($r[1])) {
            $r = explode($end, $r[1]);
            return $r[0];
        }
        return '';
    }

    private function getIsbn($url) //curl get请求
    {
        $postUrl = $url;
        $curlPost = 'GET';
        $curl = curl_init();//初始化curl
        curl_setopt($curl, CURLOPT_URL, $postUrl);//抓取指定网页
        curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        $data = curl_exec($curl);//运行curl
        curl_close($curl);
        return $data;
    }

方式二:使用第三方接口查询
极速数据

注册申请可以免费使用1000次,具体教程也很清楚图书信息相当全面
同样tips:可以将获取到图书信息存到数据库中,下次再次请求先查一遍数据库,如果有则不用爬虫,这样相对来说可以减轻压力,久而久之你也有很多图书的数据了。

在这里插入图片描述

发布了37 篇原创文章 · 获赞 13 · 访问量 4532

猜你喜欢

转载自blog.csdn.net/qq_42836388/article/details/105255714