c#使用豆瓣API

最近准备做个pivot的图书检索,所以需要从网上爬点东西,豆瓣这方面东西挺全的,而且有API,就先从豆瓣下了

虽然叫C#使用豆瓣API,其实豆瓣API主要是post和get操作,然和语言都能使用的

关于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

由于要获取图书的信息,所以先建个图书的类吧:

1   namespace doubantest
2   {
3       class bookinfo
4       {
5           private string _name;
6           private string _author;
7           private string _imageurl;
8           private string _summary;
9           private string _isbn;
10          private string _pages;
11          private string _price;
12          private string _publisher;
13          private string _pubdate;
14  
15          public bookinfo()
16          {
17              this._name = string.Empty;
18              this._author = string.Empty;
19              this._imageurl = string.Empty;
20              this._summary = string.Empty;
21              this._isbn = string.Empty;
22              this._pages = string.Empty;
23              this._price = string.Empty;
24              this._publisher = string.Empty;
25              this._pubdate = string.Empty;
26          }
27  
28          public string Name
29          {
30              get {
31                  return this._name;
32              }
33              set {
34                  this._name = value;
35              }
36          }
37  
38          public string Author
39          {
40              get
41              {
42                  return this._author;
43              }
44              set
45              {
46                  this._author = value;
47              }
48          }
49  
50          public string Imageurl
51          {
52              get
53              {
54                  return this._imageurl;
55              }
56              set
57              {
58                  this._imageurl = value;
59              }
60          }
61  
62          public string Summary
63          {
64              get
65              {
66                  return this._summary;
67              }
68              set
69              {
70                  this._summary = value;
71              }
72          }
73  
74          public string Isbn
75          {
76              get
77              {
78                  return this._isbn;
79              }
80              set
81              {
82                  this._isbn = value;
83              }
84          }
85  
86          public string Pages
87          {
88              get
89              {
90                  return this._pages;
91              }
92              set
93              {
94                  this._pages = value;
95              }
96          }
97  
98          public string Price
99          {
100             get
101             {
102                 return this._price;
103             }
104             set
105             {
106                 this._price = value;
107             }
108         }
109 
110         public string Publisher
111         {
112             get
113             {
114                 return this._publisher;
115             }
116             set
117             {
118                 this._publisher = value;
119             }
120         }
121 
122         public string Pubdate
123         {
124             get
125             {
126                 return this._pubdate;
127             }
128             set
129             {
130                 this._pubdate = value;
131             }
132         }
133 
134     }
135 }

先添加using

using System.Net;
using System.IO;
using System.Xml;

然后就是通过API获取数据了,根据豆瓣提供的格式向服务器获取数据:

1  HttpWebRequest myRequest = null;
2  HttpWebResponse myHttpResponse = null;
3  string doubanurl = "http://api.douban.com/book/subject/isbn/";
4  string geturl = doubanurl + textBoxisbnisbn.Text.ToString().Trim();
5  myRequest = (HttpWebRequest)WebRequest.Create(geturl);
6  myHttpResponse = (HttpWebResponse)myRequest.GetResponse();
7  StreamReader reader = new StreamReader(myHttpResponse.GetResponseStream());
8  string xmldetail = reader.ReadToEnd();
9  reader.Close();
10 myHttpResponse.Close();

数据获取完毕(如果输入正确的话),获得的是xml格式,内容如下:

1    <?xml version="1.0" encoding="UTF-8" ?> 
2  - <entry xmlns="http://www.w3.org/2005/Atom" xmlns:db="http://www.douban.com/xmlns/" xmlns:gd="http://schemas.google.com/g/2005"xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:opensearch="http://a9.com/-/spec/opensearchrss/1.0/">
3    <id>http://api.douban.com/book/subject/4744120</id> 
4    <title>FLUENT流体分析及仿真实用教程(附光盘)</title> 
5    <category scheme="http://www.douban.com/2007#kind" term="http://www.douban.com/2007#book" /> 
6  - <author>
7    <name>朱红钧</name> 
8    </author>
9    <link href="http://api.douban.com/book/subject/4744120" rel="self" /> 
10   <link href="http://book.douban.com/subject/4744120/" rel="alternate" /> 
11   <link href="http://img3.douban.com/pics/book-default-small.gif" rel="image" /> 
12   <summary>FLUENT是CFD软件中相对成熟和运用最为广泛的商业软件。本书以FLUENT 6.3.26版本为蓝本,由浅入深、循序渐进地介绍了利用FLUENT进行流体分析与仿真的各部分知识,包括前处理网格生成、湍流模型、传热分析、非定常流动问题、多相流模型、转动模型、组分输运与化学反应模型、流动分析后处理、UDF使用及编写等。全书通过基础知识和实例介绍相结合的方式讲解了从数值建模到计算后处理各部分操作的基本方法和步骤,最后以综合实例的方式进一步向读者介绍了典型工程问题的流体分析及仿真方法。  本书可作为水利、土木、石工、储运、航空、能源、环境、机械、建筑、海工、材料、动力、冶金等专业的研究生和本科生的计算流体力学教材,同时也可作为CFD相关领域专业技术人员的参考用书。</summary> 
13   <db:attribute name="isbn10">7115225265</db:attribute> 
14   <db:attribute name="isbn13">9787115225269</db:attribute> 
15   <db:attribute name="title">FLUENT流体分析及仿真实用教程(附光盘)</db:attribute> 
16   <db:attribute name="pages">388</db:attribute> 
17   <db:attribute name="author">朱红钧</db:attribute> 
18   <db:attribute name="price">58.0</db:attribute> 
19   <db:attribute name="publisher">人民邮电出版社</db:attribute> 
20   <db:attribute name="binding">平装</db:attribute> 
21   <db:attribute name="pubdate">2010-4-1</db:attribute> 
22   <gd:rating average="0" max="10" min="0" numRaters="0" /> 
23   </entry>

下面就是从XML里获取需要的数据:

1                  books = new bookinfo();
2  
3                  XmlDocument xml = new XmlDocument();
4                  xml.LoadXml(xmldetail);
5                  XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
6                  nsmgr.AddNamespace("db", "http://www.w3.org/2005/Atom");
7                  XmlElement root = xml.DocumentElement;
8                  XmlNodeList nodes = root.SelectNodes("/db:entry", nsmgr);
9  
10                 foreach (XmlNode nodeData in nodes)
11                 {
12                     foreach (XmlNode childnode in nodeData.ChildNodes)
13                     {
14                         string str = childnode.Name;
15                         switch (str)
16                         {
17                             case "title":
18                                 books.Name = childnode.InnerText;
19                                 break;
20                             case "link":
21                                 if (childnode.Attributes[1].Value == "image")
22                                 {
23                                     books.Imageurl = childnode.Attributes[0].Value;
24                                 }
25                                 break;
26                             case "summary":
27                                 books.Summary = childnode.InnerText;
28                                 break;
29                             case "db:attribute":
30                                 {
31                                     switch (childnode.Attributes[0].Value)
32                                     {
33                                         case "isbn13":
34                                             books.Isbn = childnode.InnerText;
35                                             break;
36                                         case"pages":
37                                             books.Pages = childnode.InnerText;
38                                             break;
39                                         case "author":
40                                             books.Author = childnode.InnerText;
41                                             break;
42                                         case "price":
43                                             books.Price = childnode.InnerText;
44                                             break;
45                                         case "publisher":
46                                             books.Publisher = childnode.InnerText;
47                                             break;
48                                         case "pubdate":
49                                             books.Pubdate = childnode.InnerText;
50                                             break;
51                                     }//end switch
52                                     break;
53                                 }
54                         }//end switch
55                     }//end foreach
56                 }//end foreach
57 
58                 labeltitle.Text = books.Name;
59                 labelauthor.Text = books.Author;
60                 labelimageurl.Text = books.Imageurl;
61                 labelisbn.Text = books.Isbn;
62                 labelpage.Text = books.Pages;
63                 labelprice.Text = books.Price;
64                 labelpublisher.Text = books.Publisher;
65                 labelpubdate.Text = books.Pubdate;
66                 labelsummary.Text = books.Summary;

最后看眼大概效果:

image

其实标准的api的URL应该是:

apikey是自己申请的

不过apikey其实没有也可以用

印象中貌似有apikey的话,没分钟允许连接40次

没有的话只能20次

超过的话会被封IP…

转自:http://www.cnblogs.com/sun8134/archive/2010/12/15/1906879.html

原文地址:https://www.cnblogs.com/jiangdd/p/2609741.html