前不久准备开只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

1 using System.Net;
2 using System.IO;
3 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/"&gt;
3    <id>http://api.douban.com/book/subject/4744120&lt;/id&gt; 
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();

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);

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;

 

末尾看眼大概效果:

图片 1

 

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

1 http://api.douban.com/book/subject/isbn/%7BisbnID%7D?apikey=%7Byourkeyapi}

 

apikey是和谐报名的

只是apikey其实没有为可为此

记忆中一般发生apikey的语,没分钟允许连接40坏

从来不底说话不过会20次

逾的话语会给封IP…

相关文章

网站地图xml地图