首先创建一个bookinfo的类:
class bookinfo
{
private string _name;
class bookinfo
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _author;
{
get { return _name; }
set { _name = value; }
}
private string _author;
public string Author
{
get { return _author; }
set { _author = value; }
}
private string _imageurl;
{
get { return _author; }
set { _author = value; }
}
private string _imageurl;
public string Imageurl
{
get { return _imageurl; }
set { _imageurl = value; }
}
private string _summary;
{
get { return _imageurl; }
set { _imageurl = value; }
}
private string _summary;
public string Summary
{
get { return _summary; }
set { _summary = value; }
}
private string _isbn;
{
get { return _summary; }
set { _summary = value; }
}
private string _isbn;
public string Isbn
{
get { return _isbn; }
set { _isbn = value; }
}
private string _pages;
{
get { return _isbn; }
set { _isbn = value; }
}
private string _pages;
public string Pages
{
get { return _pages; }
set { _pages = value; }
}
private string _price;
{
get { return _pages; }
set { _pages = value; }
}
private string _price;
public string Price
{
get { return _price; }
set { _price = value; }
}
private string _publisher;
{
get { return _price; }
set { _price = value; }
}
private string _publisher;
public string Publisher
{
get { return _publisher; }
set { _publisher = value; }
}
然后在主程序中调用bookinfo类:
HttpWebRequest myRequest = null;
HttpWebResponse myHttpResponse = null;
string doubanurl = "http://api.douban.com/book/subject/isbn/";
doubanurl = doubanurl + textBox10.Text + "?";
myRequest = (HttpWebRequest)WebRequest.Create(doubanurl);
myHttpResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myHttpResponse.GetResponseStream());
string xmldetail = reader.ReadToEnd();
reader.Close();
myHttpResponse.Close();
bookinfo books = new bookinfo();
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.Name = childnode.InnerText;
break;
case "link":
if (childnode.Attributes[1].Value == "image")
{
books.Imageurl = childnode.Attributes[0].Value;
}
break;
case "summary":
books.Summary = childnode.InnerText;
break;
case "db:attribute":
{
switch (childnode.Attributes[0].Value)
{
case "isbn13":
books.Isbn = childnode.InnerText;
break;
case "pages":
books.Pages = childnode.InnerText;
break;
case "author":
books.Author = childnode.InnerText;
break;
case "price":
books.Price = childnode.InnerText;
break;
case "publisher":
books.Publisher = childnode.InnerText;
break;
case "pubdate":
books.Pubdate = childnode.InnerText;
break;
}//end switch
break;
}
}//end switch
}//end foreach
textBox1.Text = "书名:" + books.Name;
textBox2.Text = "作者:" + books.Author;
textBox3.Text = "封面:" + books.Imageurl;
textBox4.Text = "ISBN:" + books.Isbn;
textBox5.Text = "页数:" + books.Pages;
textBox6.Text = "价格:" + books.Price;
textBox7.Text = "出版社:" + books.Publisher;
textBox8.Text = "出版日期:" + books.Pubdate;
textBox9.Text = "简介:" + books.Summary;
pictureBox1.WaitOnLoad = false;
pictureBox1.LoadAsync(books.Imageurl);//使用异步加载图书封面照片
回头写一个百度音乐的抓取类,把以前的重写一遍,哈哈
{
get { return _publisher; }
set { _publisher = value; }
}
然后在主程序中调用bookinfo类:
HttpWebRequest myRequest = null;
HttpWebResponse myHttpResponse = null;
string doubanurl = "http://api.douban.com/book/subject/isbn/";
doubanurl = doubanurl + textBox10.Text + "?";
myRequest = (HttpWebRequest)WebRequest.Create(doubanurl);
myHttpResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myHttpResponse.GetResponseStream());
string xmldetail = reader.ReadToEnd();
reader.Close();
myHttpResponse.Close();
bookinfo books = new bookinfo();
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.Name = childnode.InnerText;
break;
case "link":
if (childnode.Attributes[1].Value == "image")
{
books.Imageurl = childnode.Attributes[0].Value;
}
break;
case "summary":
books.Summary = childnode.InnerText;
break;
case "db:attribute":
{
switch (childnode.Attributes[0].Value)
{
case "isbn13":
books.Isbn = childnode.InnerText;
break;
case "pages":
books.Pages = childnode.InnerText;
break;
case "author":
books.Author = childnode.InnerText;
break;
case "price":
books.Price = childnode.InnerText;
break;
case "publisher":
books.Publisher = childnode.InnerText;
break;
case "pubdate":
books.Pubdate = childnode.InnerText;
break;
}//end switch
break;
}
}//end switch
}//end foreach
textBox1.Text = "书名:" + books.Name;
textBox2.Text = "作者:" + books.Author;
textBox3.Text = "封面:" + books.Imageurl;
textBox4.Text = "ISBN:" + books.Isbn;
textBox5.Text = "页数:" + books.Pages;
textBox6.Text = "价格:" + books.Price;
textBox7.Text = "出版社:" + books.Publisher;
textBox8.Text = "出版日期:" + books.Pubdate;
textBox9.Text = "简介:" + books.Summary;
pictureBox1.WaitOnLoad = false;
pictureBox1.LoadAsync(books.Imageurl);//使用异步加载图书封面照片
回头写一个百度音乐的抓取类,把以前的重写一遍,哈哈
谢谢你,在这世界的角落,找到我,一个即将进入而立之年的中年大叔,常年以 飞鸿踏雪 的花名混迹江湖。在现实生活中,我是一名 伪·全栈攻城狮,因为我觉得,什么都略懂一点,生活会更多彩一些。目前,主要关注.NET、.NET Core、Python、数据分析、微服务、Web 等技术方向。日常行为:读书、写作、电影、烹饪、洞箫等。喜欢看日剧/纪录片/科普、刷B站、刷LeetCode等。