java 抓取網(wǎng)頁內(nèi)容實(shí)現(xiàn)代碼
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
public class URLTest {
// 一個(gè)public方法,返回字符串,錯(cuò)誤則返回"error open url"
public static String getContent(String strUrl) {
try {
URL url = new URL(strUrl);
BufferedReader br = new BufferedReader(new InputStreamReader(url
.openStream()));
String s = "";
StringBuffer sb = new StringBuffer("");
while ((s = br.readLine()) != null) {
sb.append(s + "/r/n");
}
br.close();
return sb.toString();
} catch (Exception e) {
return "error open url:" + strUrl;
}
}
public static void initProxy(String host, int port, final String username,
final String password) {
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,
new String(password).toCharArray());
}
});
System.setProperty("http.proxyType", "4");
System.setProperty("http.proxyPort", Integer.toString(port));
System.setProperty("http.proxyHost", host);
System.setProperty("http.proxySet", "true");
}
public static void main(String[] args) throws IOException {
String url = "http://www.dbjr.com.cn";
String proxy = "http://192.168.22.81";
int port = 80;
String username = "username";
String password = "password";
String curLine = "";
String content = "";
URL server = new URL(url);
initProxy(proxy, port, username, password);
HttpURLConnection connection = (HttpURLConnection) server
.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new
InputStreamReader(is));
while ((curLine = reader.readLine()) != null) {
content = content + curLine+ "/r/n";
}
System.out.println("content= " + content);
is.close();
System.out.println(getContent(url));
}
}
- JAVA使用爬蟲抓取網(wǎng)站網(wǎng)頁內(nèi)容的方法
- java抓取網(wǎng)頁數(shù)據(jù)獲取網(wǎng)頁中所有的鏈接實(shí)例分享
- java正則表達(dá)式匹配網(wǎng)頁所有網(wǎng)址和鏈接文字的示例
- java簡單網(wǎng)頁抓取的實(shí)現(xiàn)方法
- Java中使用正則表達(dá)式獲取網(wǎng)頁中所有圖片的路徑
- java抓取網(wǎng)頁數(shù)據(jù)示例
- Java用正則表達(dá)式如何讀取網(wǎng)頁內(nèi)容
- java實(shí)現(xiàn)網(wǎng)頁解析示例
- 用javascrpt將指定網(wǎng)頁保存為Excel的代碼
- Java獲取任意http網(wǎng)頁源代碼的方法
相關(guān)文章
SpringBoot引入Thymeleaf的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot引入Thymeleaf的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Java基于Calendar類輸出指定年份和月份的日歷代碼實(shí)例
這篇文章主要介紹了Java 使用Calendar類輸出指定年份和月份的日歷,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02java性能優(yōu)化之編譯器版本與平臺對應(yīng)關(guān)系
這篇文章主要介紹了java性能優(yōu)化--編譯器版本與平臺對應(yīng)關(guān)系,本章節(jié)更加具體化的學(xué)習(xí)編譯器還有哪些可以優(yōu)化的方便,讓你的應(yīng)用展現(xiàn)出更好的性能,需要的朋友可以參考下2022-06-06springboot中RestTemplate發(fā)送HTTP請求的實(shí)現(xiàn)示例
RestTemplate是一個(gè) spring-web 提供的執(zhí)行HTTP請求的同步阻塞式工具類,本文就來介紹一下RestTemplate發(fā)送HTTP請求,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03