java 抓取網(wǎng)頁內(nèi)容實現(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 {
// 一個public方法,返回字符串,錯誤則返回"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.dhdzp.com";
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)頁中所有的鏈接實例分享
- java正則表達(dá)式匹配網(wǎng)頁所有網(wǎng)址和鏈接文字的示例
- java簡單網(wǎng)頁抓取的實現(xiàn)方法
- Java中使用正則表達(dá)式獲取網(wǎng)頁中所有圖片的路徑
- java抓取網(wǎng)頁數(shù)據(jù)示例
- Java用正則表達(dá)式如何讀取網(wǎng)頁內(nèi)容
- java實現(xiàn)網(wǎng)頁解析示例
- 用javascrpt將指定網(wǎng)頁保存為Excel的代碼
- Java獲取任意http網(wǎng)頁源代碼的方法
相關(guān)文章
@FeignClient?實現(xiàn)簡便http請求封裝方式
這篇文章主要介紹了@FeignClient?實現(xiàn)簡便http請求封裝方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot集成MQ的過程(四種交換機(jī)的實例)
本文介紹了RabbitMQ中四種交換機(jī)(直連、扇出、主題和頭交換機(jī))的使用方法,包括路由機(jī)制、典型場景和實現(xiàn)步驟,通過創(chuàng)建SpringBoot項目并配置交換機(jī)、隊列和消費者,展示了如何發(fā)送和接收消息,每種交換機(jī)的示例代碼和測試步驟也一并提供,感興趣的朋友一起看看吧2025-03-03
java中把漢字轉(zhuǎn)換成簡拼的實現(xiàn)代碼
本篇文章是對在java中把漢字轉(zhuǎn)換成簡拼的實現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
SpringCloud maven-assembly-plugin 多級目錄打包的實現(xiàn)
本文主要介紹了SpringCloud maven-assembly-plugin 多級目錄打包的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
Spring MVC訪問靜態(tài)文件_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Spring MVC訪問靜態(tài)文件的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08

