gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
用java代码写爬虫,爬取网站上的数据
- User
用java代码写爬虫,爬取网站上的数据
- Assistant
以下是一个简单的Java代码示例,用于爬取指定网站上的数据:
javaimport java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; public class WebCrawler { public static void main(String[] args) { try { // 网站URL String websiteUrl = "http://example.com"; // 创建URL对象 URL url = new URL(websiteUrl); // 打开连接 BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); String line; // 逐行读取网页内容 while ((line = reader.readLine()) != null) { // 处理每行数据,这里仅打印到控制台 System.out.println(line); } // 关闭连接 reader.close(); } catch (IOException e) { e.printStackTrace(); } } }