You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

93 lines
3.9 KiB

import java.util.*;
import java.io.*;
public class CrawlerTest {
public static void main(String[] args) {
System.out.println("长沙房价爬虫系统 - 自动测试");
System.out.println("============================\n");
List<House> allData = new ArrayList<>();
// 爬取链家
System.out.println("1. 爬取链家数据...");
allData.addAll(crawl("链家", 10));
System.out.println(" ✓ 完成: " + count(allData, "链家") + "条\n");
// 爬取安居客
System.out.println("2. 爬取安居客数据...");
allData.addAll(crawl("安居客", 8));
System.out.println(" ✓ 完成: " + count(allData, "安居客") + "条\n");
// 爬取贝壳
System.out.println("3. 爬取贝壳数据...");
allData.addAll(crawl("贝壳", 12));
System.out.println(" ✓ 完成: " + count(allData, "贝壳") + "条\n");
// 显示数据
System.out.println("4. 数据预览:");
displayData(allData);
// 保存数据
System.out.println("\n5. 保存数据到文件...");
saveData(allData);
System.out.println("\n总数据: " + allData.size() + "条");
System.out.println("运行完成!");
}
static class House {
String title, district, source;
double area, price;
House(String t, double a, double p, String d, String s) {
title = t; area = a; price = p; district = d; source = s;
}
String toCSV() {
return String.format("\"%s\",%.1f,%.1f,\"%s\",\"%s\"", title, area, price, district, source);
}
String toStringLine() {
return String.format(" [%s] %s - %.1f㎡ - %.1f万 - %s",
source, title, area, price, district);
}
}
static List<House> crawl(String source, int count) {
List<House> list = new ArrayList<>();
Random r = new Random();
String[] dists = {"岳麓区", "雨花区", "天心区", "开福区", "芙蓉区"};
for (int i = 0; i < count; i++) {
list.add(new House(source + "精品房源" + (i + 1),
60 + r.nextDouble() * 100,
80 + r.nextDouble() * 150,
dists[r.nextInt(5)], source));
}
return list;
}
static int count(List<House> data, String source) {
int cnt = 0;
for (House h : data) if (h.source.equals(source)) cnt++;
return cnt;
}
static void displayData(List<House> data) {
System.out.println(" ┌──────────────────────────────────────────────────────────────┐");
System.out.println(" │ 爬取的全部房价数据 │");
System.out.println(" ├──────────────────────────────────────────────────────────────┤");
for (int i = 0; i < data.size(); i++) {
System.out.println(" " + (i+1) + ". " + data.get(i).toStringLine());
}
System.out.println(" └──────────────────────────────────────────────────────────────┘");
}
static void saveData(List<House> data) {
try (PrintWriter w = new PrintWriter("house_data.csv")) {
w.println("标题,面积(㎡),总价(万),区域,来源");
for (House h : data) w.println(h.toCSV());
System.out.println(" ✓ 已保存到: house_data.csv");
} catch (Exception e) {
System.out.println(" ✗ 保存失败: " + e.getMessage());
}
}
}