前段时间做一个图书馆的图书录入工作,由于那边的只有图书,电子文档不全,需要对图书进行扫描然后入库,这中间就需要通过扫描ISBN号码然后把图书信息录入数据库。下面是简单的通过ISBN号来获取图书信息的操作。
首先,使用豆瓣给我们提供的API可以方便的通过ISBN获取图书信息。
关于API,详细的看豆瓣的介绍吧:
豆瓣 API :http://www.douban.com/service/apidoc/
豆瓣 API 参考手册:http://www.douban.com/service/apidoc/reference/subject#获取书籍信息
豆瓣 API 快速入门:http://www.douban.com/service/apidoc/guide
下面是我建的一个获取图书信息的类
namespace Utils
{
public class BookInfo
{
public static book GetBookByISBN(string isbn)
{
//这是自定义的一个图书类book对象
book books = new book();
try
{
//创建所需HTTP服务对象
HttpWebRequest myRequest = null;
HttpWebResponse myHttpResponse = null;
//豆瓣的获取图书信息的地址
string doubanurl = "http://api.douban.com/book/subject/isbn/";
//将图书的isbn号和申请的apikey添加到豆瓣获取图书地址的后面
//注意:?apikey=0400ce550f5955da0c026aff2e0c018e是为了使每分钟能够访问更多的次数
//可以没有,不过没有默认为每分钟访问10次,申请的话,可到豆瓣API的使用方法中了解
string geturl = doubanurl + isbn+"?apikey=0400ce550f5955da0c026aff2e0c018e";
//为Http服务对象实例化
myRequest = (HttpWebRequest)WebRequest.Create(geturl);
myHttpResponse = (HttpWebResponse)myRequest.GetResponse();
//获取网页流
StreamReader reader = new StreamReader(myHttpResponse.GetResponseStream());
//读取网页中的信息
string xmldetail = reader.ReadToEnd();
//关闭读取流
reader.Close();
//关闭Http服务
myHttpResponse.Close();
//除了book对象,下面的代码转至:http://www.cnblogs.com/sun8134/archive/2010/12/15/1906879.html
XmlDocument xml = new XmlDocument();
xml.LoadXml(xmldetail);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("db", "http://www.w3.org/2005/Atom");
XmlElement root = xml.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/db:entry", nsmgr);
foreach (XmlNode nodeData in nodes)
{
foreach (XmlNode childnode in nodeData.ChildNodes)
{
string str = childnode.Name;
switch (str)
{
case "title":
books.B_NAME = childnode.InnerText;
break;
case "link":
if (childnode.Attributes[1].Value == "image")
{
books.ImageURL = childnode.Attributes[0].Value;
}
break;
case "summary":
books.B_SUMMARY = childnode.InnerText;
break;
case "db:attribute":
{
switch (childnode.Attributes[0].Value)
{
case "isbn13":
books.B_ISBN = childnode.InnerText;
break;
case "pages":
books.B_PAGE_NUM = childnode.InnerText;
break;
case "author":
books.B_Author = childnode.InnerText;
break;
case "price":
books.B_PRICE = childnode.InnerText;
break;
case "publisher":
books.B_Publisher = childnode.InnerText;
break;
case "pubdate":
books.B_PubTime = childnode.InnerText;
break;
}//end switch
break;
}
}//end switch
}//end foreach
}//end foreach
}
catch (Exception ex)
{
}
return books;
}
}
}
book类:
public class book
{
public int B_ID { get; set; }
public string B_NO { get; set; }
public string B_NAME { get; set; }
public string B_CG_NO { get; set; }
public string B_INDEX_NO { get; set; }
public string B_PAGE_NUM { get; set; }
public string B_NUM { get; set; }
public string B_PRICE { get; set; }
public string B_Author { get; set; }
public string B_Publisher { get; set; }
public string B_PubTime { get; set; }
public string B_ISBN { get; set; }
public string B_SUMMARY { get; set; }
//额外
public string ImageURL { get; set; }
}
下面是使用情况:只要将ISBN号作为url参数传递
