?? bookschinaparser.java
字號(hào):
String bookName ="";
/* 取出<a>結(jié)點(diǎn) */
if(bookElement.hasChildNodes()
&&bookElement.getFirstChild().getNodeType() == Node.ELEMENT_NODE){
Element firstElement = (Element) bookElement.getFirstChild();
NodeList nameList = firstElement.getChildNodes();
/* 循環(huán)遍歷<a>的子結(jié)點(diǎn),并取出其中的文本值 */
for (int i = 0; i < nameList.getLength(); i++) {
Node nameNode = nameList.item(i);
/* 分元素結(jié)點(diǎn)和#text結(jié)點(diǎn)兩種不同情況進(jìn)行處理 */
if (nameNode.getNodeType() == Node.ELEMENT_NODE) {
/* 元素結(jié)點(diǎn)取得其中的文本值 */
Element nameElement = (Element) nameNode;
if(nameElement.hasChildNodes())
bookName += nameElement.getFirstChild().getNodeValue();
} else {
/* #text結(jié)點(diǎn)取得其中的文本值 */
bookName += nameNode.getNodeValue();
}
}
}
if(bookName.length()>0)
bookName = bookName.replace(" ", "");
// System.out.println(bookName);
return bookName;
}
/**
* Function: getBookAuthor
* Description: 獲得圖書作者
* Calls: no
* Called By:mainService
* @param bookElement as Element
* @return String
* @throws no
*/
public String getBookAuthor(Element bookElement) {
String bookAuthor = "";
NodeList authorList = bookElement.getChildNodes();
for (int i = 0; i < authorList.getLength(); i++) {
Node nameNode = authorList.item(i);
if (nameNode.getNodeType() == Node.ELEMENT_NODE
&& "A".equals(nameNode.getNodeName())) {
Element nameElement = (Element)nameNode;
if(nameElement.hasChildNodes())
bookAuthor += nameElement.getFirstChild().getNodeValue();
}else{
bookAuthor += nameNode.getNodeValue();
}
}
if(bookAuthor.indexOf("作者:") != -1)
bookAuthor = bookAuthor.substring(bookAuthor.indexOf("作者:")+"作者:".length());
bookAuthor=bookAuthor.replaceAll(" ", "").trim();
if(bookAuthor.length()>64)
bookAuthor=bookAuthor.substring(0, 64);
bookAuthor = bookAuthor.replace(",", " ");
bookAuthor = bookAuthor.replace(",", " ");
bookAuthor = bookAuthor.replace("等", "");
//System.out.println("bookAuthor:"+bookAuthor);
return bookAuthor;
}
/**
* Function: getBookPublisher
* Description: 獲得圖書出版社
* Calls: no
* Called By:mainService
* @param bookElement as Element
* @return String
* @throws no
*/
public String getBookPublisher(Element bookElement) {
String bookPublisher = null;
if(null != bookElement.getTextContent()&&!"".equals(bookElement.getTextContent()))
bookPublisher=bookElement.getTextContent().trim();
bookPublisher = bookPublisher.replace("出版社:", "");
//System.out.println(bookPublisher);
return bookPublisher;
}
/**
* Function: getBookPublishTime
* Description: 獲得圖書出版時(shí)間
* Calls: no
* Called By:mainService
* @param bookElement as Element
* @return String
* @throws no
*/
public String getBookPublishTime(Element bookElement) {
String bookPublishTime =null;
if(bookElement.hasChildNodes())
bookPublishTime = bookElement.getFirstChild().getNodeValue();
if(null != bookPublishTime)
bookPublishTime = bookPublishTime.trim();
if(bookPublishTime.length()>=15)
bookPublishTime = bookPublishTime.substring(0, 15);
if(null != bookPublishTime){
if(bookPublishTime.indexOf("出版日期:") != -1){
bookPublishTime = bookPublishTime.substring((bookPublishTime.indexOf("出版日期:")+"出版日期:".length()));
}
if(bookPublishTime.indexOf("ISBN:") != -1){
bookPublishTime = bookPublishTime.substring(0,bookPublishTime.indexOf("ISBN:"));
}
bookPublishTime = bookPublishTime.trim();
}
//System.out.println(bookPublishTime);
return bookPublishTime;
}
/**
* Function: getBookISBN
* Description: 獲得圖書ISBN
* Calls: no
* Called By: mainService
* @param bookElement as Element
* @return String
* @throws no
*/
public String getBookISBN(Element bookElement) {
String bookISBN =null;
if(bookElement.hasChildNodes())
bookISBN = bookElement.getFirstChild().getNodeValue();
if(bookISBN.indexOf("ISBN:")!= -1 && bookISBN.length()>bookISBN.indexOf("ISBN:") + 5)
bookISBN = bookISBN.substring(bookISBN.indexOf("ISBN:") + 5);
//System.out.println(bookISBN);
return bookISBN;
}
/**
* Function: getBookPrice
* Description: 獲得圖書網(wǎng)站價(jià)格
* Calls: no
* Called By:mainService
* @param bookElement as Element
* @return String
* @throws no
*/
public String getBookPrice(Element bookElement) {
String bookPrice = "";
NodeList authorList = bookElement.getChildNodes();
for (int i = 0; i < authorList.getLength(); i++) {
Node nameNode = authorList.item(i);
if (nameNode.getNodeType() == Node.ELEMENT_NODE
&& "SPAN".equals(nameNode.getNodeName())) {
Element nameElement = (Element)nameNode;
if(nameElement.hasChildNodes())
bookPrice = nameElement.getFirstChild().getNodeValue();
break;
}
}
bookPrice = bookPrice.replace("¥", "");
bookPrice = bookPrice.replace(",", "");
bookPrice = bookPrice.replace(",", "");
// if(bookPrice.length()>3)
// bookPrice = bookPrice.substring(2);
if(bookPrice.length()>0)
bookPrice = bookPrice.trim();
//System.out.println(bookPrice);
return bookPrice;
}
/**
* Function: getBookDiscount
* Description: 獲得圖書折扣
* Calls: no
* Called By:mainService
* @param bookElement as Element
* @return String
* @throws no
*/
public String getBookDiscount(Element bookElement) {
return null;
}
/**
* Function: getBookFixPrice
* Description: 獲得圖書定價(jià)
* Calls: no
* Called By:mainService
* @param bookElement as Element
* @return String
* @throws no
*/
public String getBookFixPrice(Element bookElement) {
String bookFixPrice = "";
NodeList authorList = bookElement.getChildNodes();
for (int i = 0; i < authorList.getLength(); i++) {
Node nameNode = authorList.item(i);
if (nameNode.getNodeType() == Node.ELEMENT_NODE) {
Element nameElement = (Element)nameNode;
if("price".equals(nameElement.getAttribute("class")))
if(nameElement.hasChildNodes())
bookFixPrice = nameElement.getFirstChild().getNodeValue();
}
}
bookFixPrice = bookFixPrice.replace("¥", "");
bookFixPrice = bookFixPrice.replace(",", "");
bookFixPrice = bookFixPrice.replace(",", "");
if(bookFixPrice.length()>0)
bookFixPrice = bookFixPrice.replace(" ", "");
// System.out.println(bookFixPrice);
return bookFixPrice;
}
/**
* Function: getBookUrl
* Description: 獲得圖書詳細(xì)信息地址
* Calls: no
* Called By:mainService
* @param bookElement as Element
* @return String
* @throws no
*/
public String getBookUrl(Element bookElement) {
String bookUrl = "";
Element firstElement = null;
if(bookElement.hasChildNodes()){
if(bookElement.getFirstChild().getNodeType() == Node.ELEMENT_NODE){
firstElement = (Element) bookElement.getFirstChild();
if(null != firstElement.getAttribute("href"))
bookUrl = "http://www.bookschina.com"
+ firstElement.getAttribute("href").trim();
}
}
//System.out.println(bookUrl);
return bookUrl;
}
public String getBookContent(Element bookElement) {
// TODO Auto-generated method stub
return null;
}
/**
* Function: getNextPageUrl
* Description: 獲得下一頁(yè)超鏈接地址
* Calls: no
* Called By: no
* @param doc as Document
* @return String
* @throws no
*/
public String getNextPageUrl(Document doc) {
/* 初始化為no,表示沒有下一頁(yè) */
String nextpageUrl = "no";
NodeList divList = doc.getElementsByTagName("div");
for (int i = 0; i < divList.getLength(); i++) {
if(divList.item(i).getNodeType() == Node.ELEMENT_NODE){
Element serveritem = (Element) divList.item(i);
/* 過濾出分頁(yè)工具欄標(biāo)簽<div id="divBottomPageNavi"...> */
if ("float: right;margin:5px 5px 0px 0px;".equals(serveritem
.getAttribute("style"))) {
NodeList chList = serveritem.getChildNodes();
if(chList.getLength()>=5){
Element sElement = (Element) chList.item(4);
if(null != sElement.getAttribute("href")
&& !"".equals(sElement.getAttribute("href"))){
nextpageUrl = "http://www.bookschina.com"
+ sElement.getAttribute("href").trim();
break;
}
}
}
}
}
//System.out.println(nextpageUrl);
return nextpageUrl;
}
public long getRecordNum(Document doc) {
/* 初始化為0 */
long num = 0;
NodeList servers = doc.getElementsByTagName("div");
for (int i = 0; i < servers.getLength(); i++) {
if(servers.item(i).getNodeType()==Node.ELEMENT_NODE){
Element serveritem = (Element) servers.item(i);
if ("keyword".equals(serveritem.getAttribute("class"))) {
NodeList childList1 = serveritem.getChildNodes();
for(int j = 0;j<childList1.getLength();j++){
Node spanNode = childList1.item(j);
if(spanNode.getNodeType()== Node.ELEMENT_NODE){
Element spanElement = (Element) spanNode;
if("topic2".equals(spanElement.getAttribute("class"))){
if(null != spanElement.getFirstChild().getNodeValue())
num = Long.valueOf(spanElement.getFirstChild().getNodeValue().trim());
break;
}
}
}
}
}
}
//System.out.println(num);
return num;
}
public static void main(String args[]) throws Exception {
//System.out.println("作 者:".length());
BookschinaParser bookChina = new BookschinaParser();
Document doc = bookChina
.nekohtmlParser("http://www.bookschina.com/book_find/goodsfind.aspx?book=java");
//Price price = bookChina.getDetailInfo(doc);
//System.out.println(price.getBookschinaDiscount() + ">>" + price.getBookschinaPrice() + price.getBookschinaUrl());
ArrayList<Book> list=bookChina.mainService(doc,true);
Iterator it=list.iterator();
while(it.hasNext())
{
Book book1=(Book)it.next();
System.out.println(book1.getBookName()+">>" + book1.getBookAuthor() + ">>" + book1.getBookImage() +">>"+book1.getBookFixPrice()+">>"+book1.getBookISBN()+">>"+book1.getBookPublishTime()+">>"+book1.getBookPublisher());
Price price = book1.getPrice();
System.out.println(price.getBookschinaDiscount()+">>"+price.getBookschinaPrice()+">>" + price.getBookschinaUrl());
}
System.out.println(bookChina.getNextPageUrl(doc));
// System.out.println(dangDang.getNextPageUrl(doc));
System.out.println(bookChina.getRecordNum(doc));
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -