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

