网咯爬虫java基础样例
package com.liangrui;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class InternetReptileTest {
public static void main(String[] args) throws IOException {
URL url =new URL("https://www.dianping.com");
HttpURLConnection conn =(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.50");
BufferedReader br =new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
String msg =null;
while(null!=(msg=br.readLine())) {
System.out.println(msg);
}
br.close();
}
}
到此,网咯爬虫java基础样例已完成。有问题随时评论,觉得有用的话记得点个赞哦!
在代码实例中学习知识,学和用兼备,想要长期学习的小伙伴记得关注哦!