由ISBN自动获取书籍信息——下载可用
首先需要安装依赖环境,因为代码运行的时候会用到org.apache.http
顺便放一个net.sf.json的依赖包
整理这篇博客的目的是网上的代码有的缺一个类,有的依赖库没给,所以在学的时候费了一番功夫,特此整理。
首先是书本保存的信息类
/*** */
package test;/*** @author ly**/
import java.io.Serializable;public class TudouBookInfo implements Serializable {private static final long serialVersionUID = 2179631010054135058L;private String tags;//书本标签private String isbn10;//10位ISBNprivate String isbn13;private String title;private String pages;private String author;private String price;private String binding;private String publisher;private String pubdate;private String summary;private String imagePath;/*** @return the imagePath*/public String getImagePath() {return imagePath;}/*** @param imagePath* the imagePath to set*/public void setImagePath(String imagePath) {this.imagePath = imagePath;}public TudouBookInfo() {}/*** @return the tags*/public String getTags() {return tags;}/*** @param tags* the tags to set*/public void setTags(String tags) {this.tags = tags;}/*** @return the isbn10*/public String getIsbn10() {return isbn10;}/*** @param isbn10* the isbn10 to set*/public void setIsbn10(String isbn10) {this.isbn10 = isbn10;}/*** @return the isbn13*/public String getIsbn13() {return isbn13;}/*** @param isbn13* the isbn13 to set*/public void setIsbn13(String isbn13) {this.isbn13 = isbn13;}/*** @return the title*/public String getTitle() {return title;}/*** @param title* the title to set*/public void setTitle(String title) {this.title = title;}/*** @return the pages*/public String getPages() {return pages;}/*** @param pages* the pages to set*/public void setPages(String pages) {this.pages = pages;}/*** @return the author*/public String getAuthor() {return author;}/*** @param author* the author to set*/public void setAuthor(String author) {this.author = author;}/*** @return the price*/public String getPrice() {return price;}/*** @param price* the price to set*/public void setPrice(String price) {this.price = price;}/*** @return the binding*/public String getBinding() {return binding;}/*** @param binding* the binding to set*/public void setBinding(String binding) {this.binding = binding;}/*** @return the publisher*/public String getPublisher() {return publisher;}/*** @param publisher* the publisher to set*/public void setPublisher(String publisher) {this.publisher = publisher;}/*** @return the pubdate*/public String getPubdate() {return pubdate;}/*** @param pubdate* the pubdate to set*/public void setPubdate(String pubdate) {this.pubdate = pubdate;}/*** @return the summary*/public String getSummary() {return summary;}/*** @param summary* the summary to set*/public void setSummary(String summary) {this.summary = summary;}}
写一个XML的解析类
通过ISBN查询得到的信息以XML格式的形式返回,所以写一个针对此类XML格式解析的类才能得到我们想要的具体的信息(如:标题,作者,简介等等)。
/*** */
package test;/*** @author ly**/
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;public class BookXMLParser extends DefaultHandler {private TudouBookInfo book = null;private final StringBuilder buff = new StringBuilder();private String attname = null;private final List<String> tags = new ArrayList<String>();/*** @return the book*/public TudouBookInfo getBook() {return book;}public BookXMLParser(InputStream is) {try {SAXParserFactory spfactory = SAXParserFactory.newInstance();spfactory.setValidating(false);SAXParser saxParser = spfactory.newSAXParser();XMLReader xmlReader = saxParser.getXMLReader();xmlReader.setContentHandler(this);xmlReader.parse(new InputSource(is));} catch (Exception e) {System.err.println(e);System.exit(1);}}public void startElement(String uri, String localName, String name,Attributes atts) throws SAXException {if (name.equalsIgnoreCase("entry")) {book = new TudouBookInfo();} else if (name.equalsIgnoreCase("db:attribute")) {attname = atts.getValue("name");} else if (name.equalsIgnoreCase("db:tag")) {tags.add(atts.getValue("name"));} else if (name.equalsIgnoreCase("link")) {if ("image".equalsIgnoreCase(atts.getValue("rel"))) {book.setImagePath(atts.getValue("href"));}}buff.setLength(0);}public void endElement(String uri, String localName, String name)throws SAXException {if ("entry".equalsIgnoreCase(name)) {StringBuilder str = new StringBuilder();for (String t : tags) {str.append(t + "/");}book.setTags(str.toString());} else if (name.equalsIgnoreCase("db:attribute")) {String value = buff.toString().trim();if ("isbn10".equalsIgnoreCase(attname)) {book.setIsbn10(value);} else if ("isbn13".equalsIgnoreCase(attname)) {book.setIsbn13(value);} else if ("title".equalsIgnoreCase(attname)) {book.setTitle(value);} else if ("pages".equalsIgnoreCase(attname)) {book.setPages(value);} else if ("author".equalsIgnoreCase(attname)) {book.setAuthor(value);} else if ("price".equalsIgnoreCase(attname)) {book.setPrice(value);} else if ("publisher".equalsIgnoreCase(attname)) {book.setPublisher(value);} else if ("binding".equalsIgnoreCase(attname)) {book.setBinding(value);} else if ("pubdate".equalsIgnoreCase(attname)) {book.setPubdate(value);}} else if ("summary".equalsIgnoreCase(name)) {book.setSummary(buff.toString());}buff.setLength(0);}public void characters(char ch[], int start, int length)throws SAXException {buff.append(ch, start, length);}}
**最后写一个测试类 **
/*** */
package test;/*** @author ly**/
import java.io.IOException;
import java.io.InputStream;import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;public class RetrieveDocumentByURL {public RetrieveDocumentByURL(String url) throws ClientProtocolException, IOException{DefaultHttpClient client = new DefaultHttpClient();HttpGet get = new HttpGet(url);HttpResponse response = client.execute(get);HttpEntity entity = response.getEntity();InputStream is = entity.getContent();TudouBookInfo book = new BookXMLParser(is).getBook();System.out.println("title:->" + book.getTitle());System.out.println("summary:->"+ book.getSummary());System.out.println("price:-->" + book.getPrice());System.out.println("author:-->" + book.getAuthor());System.out.println("ImagePath:-->" + book.getImagePath());}public static void main(String[] args) throws ClientProtocolException, IOException {new RetrieveDocumentByURL("http://api.douban.com/book/subject/isbn/9787121194276");}}
最后,如何自动获得ISBN呢?其实用QQ,微信的扫一扫对着ISBN的二维码扫一下就可以实现,但是QQ扫出来是一串ISBN号,而微信扫出来是完整的书籍信息
由ISBN自动获取书籍信息——下载可用相关推荐
- 【JAVA】通过ISBN一键获取书籍信息
package api;import java.util.Vector;import javax.swing.JOptionPane;import net.sf.json.JSONArray; imp ...
- 站长导航系统源码 二开优化 美观自动审核 自动获取网站信息
介绍: 站长导航系统源码 二开优化 美观自动审核 自动获取网站信息 网盘下载地址: http://www.bytepan.net/7KOqz7bmN33 图片:
- 可以模拟人工操作的软件;如访问网页,在网页中自动获取固定信息等
有什么模拟人工操作的软件比较强大,如可以添加执行操作步骤,访问网站,在网页中自动获取固定信息等:在线等,跪求大神帮助
- springboot使用JWT,并自动获取用户信息
使用JWT生成token,并在controller中通过注解判断登录权限,自动在需要登录的api中获取用户信息,支持分布式登录. 废话不多说,直接上链接sunxy0617/jinwu_admin: J ...
- 干货全拿走-用Excel实现自动获取基金行情及下载历史数据
这一篇是对我之前股票和期货网抓的补充,如对股票和期货抓取感兴趣,可以参考我的上篇文章:用Excel实现自动获取期货.期权.股票行情及下载历史数据 - 知乎.在做完股票和期货的网抓后,感觉基金也可以一试 ...
- 使用oauth2.0自动获取用户信息
开发过程中,在未使用oauth2.0之前,通常确定用户信息是使用推送消息并带有用户 openid 来实现的,但带来的问题也很明显,如果是用户主动分享出去由其他用户点击进入的则无法正常获取其基本信息,如 ...
- python 小说下载_通过python自动获取小说并下载
1 importurllib.request2 importos3 4 headers ={5 "User-Agent": "Mozilla/5.0 (Windows N ...
- 通过豆瓣Api,输入ISBN获取图书信息
在本篇文章中,主要是通过豆瓣API实现获取图书信息的小功能. 一. 豆瓣API能干什么? 参考链接:[url]http://www.douban.com/service/ [/url] 豆瓣API ...
- 下载华为交换机 MIB 参考文件并使用 snmpwalk 获取 OID 信息
这里填写标题 1. 下载华为交换机 MIB 参考文件并使用 snmpwalk 获取 OID 信息 1. 下载华为交换机 MIB 参考文件并使用 snmpwalk 获取 OID 信息 下载交换机 MIB ...
最新文章
- Vs2008不能调试的问题
- linux:scp从入门到刚入门
- 【NLP】NER数据标注中的标签一致性验证
- C#LeetCode刷题-队列
- Flask 发布 1.0 稳定版
- c语言生日创意代码_C语言如何编程生日快乐代码
- linux文件的上传和下载(终端工具SCRT和XShell)
- STM32烧写程序:Keil5使用ST-link下载程序
- matlab 上三角矩阵变为对称矩阵,已知上/下三角矩阵如何快速将对称阵补全
- 利用Visio DIY自己的示意图
- python判断是否为素数_python判断一个数是否为素数
- shared_preferences本地存储操作
- effect和watch 的区别详解
- 国内有哪些小众但很有意思的网站?这6个网站值得收藏
- 换发型特效怎么制作?建议收藏这些方法
- 【每日早报】2019/12/31
- 马云谈大数据:数据时代的“五个新” 做好准备
- transmac装黑苹果_自己安装黑苹果,安装mac简单教程,双系统轻松使用
- 打造全球TOD典范城市!成都准备这么干
- decorate怎么读(decorated怎么读)
热门文章
- python run什么都没有_求助大佬问题:运行代码之后什么都没有显示什么情况?...
- python2.x 与python3.x之d.keys()返回类型的区别
- 使用wireshark查看TCP三次握手
- Visual C++中的C运行时库浅析
- P32 批量插入数据
- python:输出内容对不齐怎么办?这篇文章解决大家的数据输出对不齐,不好看的问题
- java buffer 记事本_GitHub - programmer-zhang/DuangCalender: 简易日历记事本(java+android端)...
- kafka集成后台代码整理(支持kafka集群)
- 酷家乐x极盾科技:“智能安全决策平台”助力日均十亿级日志分析
- C-V2X(四)C-V2X模组