⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 courtnews.java

📁 ajax lucene 部分源代码 HTMLParser.java MuiltiSearchTest.java
💻 JAVA
字号:
package test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Segment;
import net.htmlparser.jericho.Source;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

 

/** */
/**
 * @author oscar 07-5-17
 * 
 */
public class CourtNews {
	private int newsCount = 3;

	private List newsList = new ArrayList();

	public int getNewsCount() {
		return newsCount;
	}

	public void setNewsCount(int newsCount) {
		this.newsCount = newsCount;
	}

	public List getNewsList() {
		HttpClient httpClient = new HttpClient();
		GetMethod getMethod = new GetMethod(
				"http://www.ahcourt.gov.cn/gb/ahgy_2004/fyxw/index.html");
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler());

		try {
			int statusCode = httpClient.executeMethod(getMethod);
			if (statusCode != HttpStatus.SC_OK) {
				System.err
						.println("Method failed:" + getMethod.getStatusLine());
			}

			String responseBody = getMethod.getResponseBodyAsString();
			responseBody = new String(responseBody.getBytes("ISO-8859-1"),
					"GB2312");
			Source source = new Source(responseBody);

			int tableCount = 0;

			for (Iterator i = source.findAllElements(HTMLElementName.TABLE)
					.iterator(); i.hasNext(); tableCount++) {

				Segment segment = (Segment) i.next();

				if (tableCount == 13) {

					int hrefCount = 0;
					for (Iterator j = segment
							.findAllElements(HTMLElementName.A).iterator(); j
							.hasNext();) {
						Segment childsegment = (Segment) j.next();
						String title = childsegment.extractText();
						title.replace(" ", " ");
						title = trimTitle(title);
						Element childelement = (Element) childsegment;
						if (hrefCount < newsCount) {

							String[] news = new String[] {
									title,
									"http://www.ahcourt.gov.cn"
											+ childelement
													.getAttributeValue("href") };
							newsList.add(news);
							hrefCount++;
						}
					}

				}
			}
		} catch (HttpException e) {
			System.out.println("please check your provided http address!");
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			getMethod.releaseConnection();
		}
		return newsList;
	}

	private String trimTitle(String title) {
		String titlenew = "";

		for (int i = 0; i < title.length(); i++) {

			if (Character.isSpaceChar(title.charAt(i)))
				titlenew += " ";
			else {
				titlenew += title.charAt(i);
			}

		}
		return titlenew;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CourtNews justice = new CourtNews();
		justice.setNewsCount(4);
		List list = justice.getNewsList();
		Iterator it = list.iterator();
		while (it.hasNext()) {
			String[] news = (String[]) it.next();
			System.out.println(news[0]);
			System.out.println(news[1]);
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -