中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

java采集網(wǎng)頁(yè) 抓取網(wǎng)頁(yè)

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * java采集網(wǎng)頁(yè)
 * 
 */
public class HttpWebCollecter {

	/**
	 * 網(wǎng)頁(yè)抓取方法
	 * 
	 * @param urlString
	 *            要抓取的url地址
	 * @param charset
	 *            網(wǎng)頁(yè)編碼方式
	 * @param timeout
	 *            超時(shí)時(shí)間
	 * @return 抓取的網(wǎng)頁(yè)內(nèi)容
	 * @throws IOException
	 *             抓取異常
	 */
	public static String GetWebContent(String urlString, final String charset,
			int timeout) throws IOException {
		if (urlString == null || urlString.length() == 0) {
			return "";
		}
		urlString = (urlString.startsWith("http://") || urlString
				.startsWith("https://")) ? urlString : ("http://" + urlString)
				.intern();
		URL url = new URL(urlString);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoOutput(true);
		conn.setRequestProperty("Pragma", "no-cache");
		conn.setRequestProperty("Cache-Control", "no-cache");

		int temp = Integer.parseInt(Math.round(Math.random()
				* (UserAgent.length - 1))
				+ "");
		conn.setRequestProperty("User-Agent", UserAgent[temp]); // 模擬手機(jī)系統(tǒng)
		conn.setRequestProperty("Accept",
				"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");// 只接受text/html類型,當(dāng)然也可以接受圖片,pdf,*/*任意,就是tomcat/conf/web里面定義那些
		conn.setConnectTimeout(timeout);
		try {
			if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
				return "";
			}
		} catch (Exception e) {
			try {
				System.out.println(e.getMessage());
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			return "";
		}
		InputStream input = conn.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(input,
				charset));
		String line = null;
		StringBuffer sb = new StringBuffer("");
		while ((line = reader.readLine()) != null) {
			sb.append(line).append("\r\n");
		}
		if (reader != null) {
			reader.close();
		}
		if (conn != null) {
			conn.disconnect();
		}
		return sb.toString();
	}

	public static String[] UserAgent = {
			"Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.2",
			"Mozilla/5.0 (iPad; U; CPU OS 3_2_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B500 Safari/531.21.11",
			"Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18121",
			"Nokia5700AP23.01/SymbianOS/9.1 Series60/3.0",
			"UCWEB7.0.2.37/28/998", "NOKIA5700/UCWEB7.0.2.37/28/977",
			"Openwave/UCWEB7.0.2.37/28/978",
			"Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/989" };

}

標(biāo)簽: linux

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:java文本文件加密解密類

下一篇:在指定的范圍內(nèi),生成不重復(fù)的隨機(jī)數(shù)序列(排除法,篩選法)