获取个人借阅信息---图书馆client

在上一篇利用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); } } }


測试结果:



待续……

原文地址:https://www.cnblogs.com/claireyuancy/p/6958691.html