1. 程式人生 > >獲取個人借閱信息---圖書館client

獲取個人借閱信息---圖書館client

execute name tar col cati library args null href

在上一篇利用jsoup解析個人信息----圖書館client,獲得個人基本信息後。便有了進一步的需求:獲取當前借閱的具體信息

獲取的方法還是一樣的。利用jsoup解析借閱信息頁面,獲得所需數據,封裝成LendBookInfo,然後將其增加一個List中。

借閱信息詳情頁例如以下:

技術分享

模擬get請求得到其html字符串。代碼較簡單

/**
	 *獲取當前借閱信息。 必須在login()調用之後
	 * 
	 * @return
	 */
	public static String getCurLendInfo() {
		String curLendInfo = null;

		/**
		 * 
		 * location----------- /patroninfo~S3*chx/1****82/top
		 * 
		 * 目標-------------/patroninfo~S3*chx/1****82/items
		 * 
		 * tem_location----/patroninfo~S3*chx/1****82
		 * 
		 * 
		 */
		HttpGet httpget = null;
		String tem_location = location.substring(0, location.lastIndexOf("/"));
		System.out.println("tem_location---->" + tem_location);

		try {
			httpget = new HttpGet(baseUrl + tem_location + "/items");
			response = httpclient.execute(httpget);// 發送get請求

			int code = response.getStatusLine().getStatusCode();
			System.out.println(response.getStatusLine());
			if (code == 200) {
				if (response != null) {
					curLendInfo = EntityUtils.toString(response.getEntity(),
							HTTP.UTF_8);
				}
			}
		} catch (ClientProtocolException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}

		return curLendInfo;

	}


得到html後。便能夠利用jsoup進行解析。

打開firedebug。進行分析。

代碼例如以下:

	/**
	 * 獲取借閱的數目信息
	 * 
	 * @param lendInfoHtml
	 *            借閱信息詳情html
	 * @return 借閱信息列表
	 */
	public static List<LendBookInfo> getLendBookInfos(String lendInfoHtml) {

		List<LendBookInfo> lendBookInfos = new ArrayList<>();
		Document document = Jsoup.parse(lendInfoHtml);
		Element table = document.getElementsByClass("patFunc").get(0);// 表格
		Elements items = table.getElementsByClass("patFuncEntry");// 數目信息集合
		for (Element item : items) {

			LendBookInfo bookInfo = null;

			Element ele_title = item.getElementsByClass("patFuncTitle").get(0);// 題名
			String bookDetail = ele_title.child(0).text();

			Element ele_barCode = item.getElementsByClass("patFuncBarcode")
					.get(0);// 條形碼
			String barCode = ele_barCode.text();

			Element ele_status = item.getElementsByClass("patFuncStatus")
					.get(0);// 狀態
			String status = ele_status.text();

			Element ele_callNumber = item.getElementsByClass("patFuncCallNo")
					.get(0);// 索書號
			String callNumber = ele_callNumber.text();

			bookInfo = new LendBookInfo(bookDetail, callNumber, status, barCode);
			lendBookInfos.add(bookInfo);
		}
		return lendBookInfos;

	}

測試例如以下:


public static void main(String[] args) {

		boolean isConn = LibraryUtil.login(stuNo, password);

		/**
		 * 若登陸成功則將信息保存到數據庫(學號、密碼須要加密)。

*/ if (isConn) { String resultHtml = LibraryUtil.getResultHtml(); UserInfo userInfo = UserInfoHandler.getUserInfo(resultHtml); userInfo.setStuNo(stuNo); userInfo.setPassword(password); System.out.println("========"); System.out.println(userInfo.toString()); String lendInfoHtml = LibraryUtil.getCurLendInfo(); List<LendBookInfo> lendBookInfos = UserInfoHandler .getLendBookInfos(lendInfoHtml); for (LendBookInfo bookInfo : lendBookInfos) { System.out.println(bookInfo); } } }


測試結果:


技術分享


待續……

獲取個人借閱信息---圖書館client