package com.xunlei;

import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @author Hank
 * @date 2015-4-22 下午4:25:22
 */
public class XunleihuiyuanNet {
	private String txtFile1 = "D:\\sourcefile_1.txt";
	private String txtFile2 = "D:\\sourcefile_2.txt";

	/*public static void main(String[] args) {
		XunleihuiyuanNet xunleihuiyuanNet = new XunleihuiyuanNet();
		String url = "http://www.xunleihuiyuan.net/";
		xunleihuiyuanNet.save1(url);
		String href = xunleihuiyuanNet.jx1(url);
		xunleihuiyuanNet.save2(href);
		String result = xunleihuiyuanNet.jx2();
		System.out.println(result);
	}*/

	//保存首页源码
	public void save1(String url) {
		HttpConnection httpConnection = new HttpConnection();
		httpConnection.createConnection(url, "GET", 7000);// 创建连接
		setConn(httpConnection.conn);// 设置连接
		httpConnection.outPut(httpConnection.getByte(), txtFile1);// 获得连接byte数组,并保存
	}
	
	//保存提取页源码
	public void save2(String href) {
		HttpConnection httpConnection = new HttpConnection();
		httpConnection.createConnection(href, "GET", 7000);// 创建连接
		setConn(httpConnection.conn);// 设置连接
		httpConnection.outPut(httpConnection.getByte(), txtFile2);// 获得连接byte数组,并保存
	}

	//解析首页提取想要的url地址
	public String jx1(String url) {
		HttpConnection httpConnection = new HttpConnection();
		try {
			Document document = Jsoup.parse(new String(httpConnection.inPut(txtFile1), "UTF-8"));
			Element element = document.getElementById("divMain");
			Elements elements = element.children();
			for (Element e : elements) {
				Elements es = e.getElementsByTag("h3");
				if (es != null && es.size() > 0) {
					element = e.getElementsByTag("h2").get(0).getElementsByTag("a").get(0);
					String href = element.attr("href");
					if (!href.startsWith("http://"))
						href = url + href;
					return href;
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	//解析页面提前想要的信息
	public String jx2() {
		HttpConnection httpConnection = new HttpConnection();
		StringBuffer sb = new StringBuffer();
		try {
			Document document = Jsoup.parse(new String(httpConnection.inPut(txtFile2), "UTF-8"));
			Element element = document.getElementById("divMain");
			Elements elements = element.children().get(0).children();
			// String txt = element.children().get(0).children().get(5).text();
			String txt = null;
			for (Element e : elements) {
				if ("post-body formattext".equals(e.attr("class"))) {
					txt = e.text();
					break;
				}
			}
			//
			// System.out.println(element.getElementsByClass("post-body formattext").size());
			// System.out.println(element.children().get(0).children().get(5).attr("class"));
			String[] txts = txt.split(" ");
			for (String str : txts)
				sb.append(str + "\r\n");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	/**
	 * 设置连接
	 */
	public void setConn(HttpURLConnection conn) {
		conn.setRequestProperty("Accept", "*/*");
		conn.setRequestProperty("Connection", "keep-alive");
		conn.setRequestProperty("Host", "www.xunleihuiyuan.net");
		conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36");
	}
}
最近下载更多
gan857569302  LV9 2020年7月15日
xsj123456  LV1 2019年10月25日
17600446733  LV21 2019年6月11日
lris_luanling  LV11 2018年11月27日
1324488732  LV27 2018年11月22日
温涛涛  LV8 2018年11月5日
040413  LV3 2018年10月22日
dffdfdaf  LV3 2018年10月17日
chenzhen123456  LV9 2018年9月25日
murphyLu  LV12 2018年8月28日
最近浏览更多
zxf2342  LV2 2023年4月27日
猫和老鼠sa 2022年12月2日
暂无贡献等级
山鬼  LV2 2021年12月8日
a320575517 2021年11月24日
暂无贡献等级
1798672867  LV21 2021年7月4日
David 2021年7月2日
暂无贡献等级
1964341683 2021年6月21日
暂无贡献等级
Sean_admin  LV7 2021年4月28日
1530688385  LV5 2020年10月22日
397312607  LV2 2020年9月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友