补充客户信息
This commit is contained in:
114
src/test/java/com/rj/gaode/AllShopCollectorV5.java
Normal file
114
src/test/java/com/rj/gaode/AllShopCollectorV5.java
Normal file
@@ -0,0 +1,114 @@
|
||||
package com.rj.gaode;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
public class AllShopCollectorV5 {
|
||||
|
||||
// ==================== 只需要改这里 5 项 ====================
|
||||
private static final String AMAP_KEY = "34eaa47387aa9e8c1a8ca0d4c8abaa1d"; // 替换成你的Key
|
||||
private static final String CITY = "河北省廊坊市三河市燕郊镇"; // 城市
|
||||
private static final String ADDRESS = "燕顺路星河皓月小区"; // 地标/街道/路(自动转经纬度)
|
||||
private static final int RADIUS = 800; // 半径800米(可改300/500/1000)
|
||||
private static final int MAX_PAGE = 2; // 采集页数
|
||||
// ==========================================================
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
System.out.println("正在获取地址:" + ADDRESS + " 的经纬度...");
|
||||
String location = getLocationByAddress(CITY, ADDRESS);
|
||||
System.out.println("经纬度获取成功:" + location);
|
||||
|
||||
// 开始全行业采集
|
||||
for (int page = 1; page <= MAX_PAGE; page++) {
|
||||
System.out.println("\n===== 全量采集第 " + page + " 页商家 =====");
|
||||
String json = getAllShops(location, page);
|
||||
parseAndPrint(json);
|
||||
Thread.sleep(800); // 风控间隔,稳定不封号
|
||||
}
|
||||
System.out.println("\n===== 全行业商家采集完成 =====");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. 地址自动转经纬度(核心:不用手动打开网页)
|
||||
*/
|
||||
public static String getLocationByAddress(String city, String address) throws Exception {
|
||||
String url = "https://restapi.amap.com/v3/geocode/geo"
|
||||
+ "?key=" + AMAP_KEY
|
||||
+ "&city=" + URLEncoder.encode(city, "UTF-8")
|
||||
+ "&address=" + URLEncoder.encode(address, "UTF-8")
|
||||
+ "&output=json";
|
||||
String json = sendGet(url);
|
||||
int start = json.indexOf("\"location\":\"") + 12;
|
||||
int end = json.indexOf("\"", start);
|
||||
return json.substring(start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* 2. 【不指定行业】搜索周边所有商家(核心)
|
||||
*/
|
||||
public static String getAllShops(String location, int page) throws Exception {
|
||||
String url = "https://restapi.amap.com/v3/place/around" // 周边搜索(无关键词=全行业)
|
||||
+ "?key=" + AMAP_KEY
|
||||
+ "&location=" + location // 中心点经纬度
|
||||
+ "&radius=" + RADIUS // 半径范围
|
||||
+ "&offset=50" // 每页50条
|
||||
+ "&page=" + page // 页码
|
||||
+ "&output=json";
|
||||
return sendGet(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP请求工具
|
||||
*/
|
||||
public static String sendGet(String url) throws Exception {
|
||||
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||
String inputLine;
|
||||
StringBuilder content = new StringBuilder();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
content.append(inputLine);
|
||||
}
|
||||
in.close();
|
||||
conn.disconnect();
|
||||
return content.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析所有商家信息
|
||||
*/
|
||||
public static void parseAndPrint(String json) {
|
||||
String[] shops = json.split("\"name\":\"");
|
||||
for (int i = 1; i < shops.length; i++) {
|
||||
String s = shops[i];
|
||||
String name = get(s, "");
|
||||
String addr = get(s, "address\":\"");
|
||||
String tel = get(s, "tel\":\"");
|
||||
String type = get(s, "type\":\"");
|
||||
String loc = get(s, "location\":\"");
|
||||
|
||||
System.out.println("店名:" + name);
|
||||
System.out.println("地址:" + addr);
|
||||
System.out.println("电话:" + tel);
|
||||
System.out.println("行业:" + type);
|
||||
System.out.println("坐标:" + loc);
|
||||
System.out.println("---------------------------");
|
||||
}
|
||||
}
|
||||
|
||||
private static String get(String s, String pre) {
|
||||
try {
|
||||
int i = s.indexOf(pre) + pre.length();
|
||||
return s.substring(i, s.indexOf("\"", i));
|
||||
} catch (Exception e) {
|
||||
return "无";
|
||||
}
|
||||
}
|
||||
}
|
||||
133
src/test/java/com/rj/gaode/AutoLocationCollector.java
Normal file
133
src/test/java/com/rj/gaode/AutoLocationCollector.java
Normal file
@@ -0,0 +1,133 @@
|
||||
package com.rj.gaode;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
public class AutoLocationCollector {
|
||||
|
||||
// ==================== 配置区(只改这里)====================
|
||||
private static final String AMAP_KEY = "你的Web服务Key";
|
||||
private static final String CITY = "杭州市"; // 城市
|
||||
private static final String ADDRESS = "万达广场"; // 地标/街道/路/建筑(自动转经纬度)
|
||||
private static final String KEYWORDS = "餐饮"; // 招商行业
|
||||
private static final String TYPES = "050000"; // 行业编码
|
||||
private static final int RADIUS = 800; // 半径800米
|
||||
private static final int MAX_PAGE = 3; // 采集页数
|
||||
// ==========================================================
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
System.out.println("正在获取地址:" + ADDRESS + " 的经纬度...");
|
||||
// 1. 自动获取经纬度
|
||||
String location = getLocationByAddress(CITY, ADDRESS);
|
||||
System.out.println("获取成功:经纬度 = " + location);
|
||||
|
||||
// 2. 自动采集周边商家
|
||||
for (int page = 1; page <= MAX_PAGE; page++) {
|
||||
System.out.println("\n===== 正在采集第 " + page + " 页商家 =====");
|
||||
String json = getShopList(location, page);
|
||||
parseAndPrint(json);
|
||||
Thread.sleep(800);
|
||||
}
|
||||
System.out.println("\n===== 全自动招商采集完成 =====");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 高德地理编码API:地址 → 自动获取经纬度
|
||||
*/
|
||||
public static String getLocationByAddress(String city, String address) throws Exception {
|
||||
String url = "https://restapi.amap.com/v3/geocode/geo"
|
||||
+ "?key=" + AMAP_KEY
|
||||
+ "&city=" + URLEncoder.encode(city, "UTF-8")
|
||||
+ "&address=" + URLEncoder.encode(address, "UTF-8")
|
||||
+ "&output=json";
|
||||
|
||||
String json = sendHttpRequest(url);
|
||||
return extractLocation(json);
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用POI搜索,获取周边商家
|
||||
*/
|
||||
public static String getShopList(String location, int page) throws Exception {
|
||||
String url = "https://restapi.amap.com/v3/place/text"
|
||||
+ "?key=" + AMAP_KEY
|
||||
+ "&city=" + URLEncoder.encode(CITY, "UTF-8")
|
||||
+ "&keywords=" + URLEncoder.encode(KEYWORDS, "UTF-8")
|
||||
+ "&types=" + TYPES
|
||||
+ "&location=" + location
|
||||
+ "&radius=" + RADIUS
|
||||
+ "&citylimit=true"
|
||||
+ "&offset=50"
|
||||
+ "&page=" + page
|
||||
+ "&output=json";
|
||||
return sendHttpRequest(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一HTTP请求工具
|
||||
*/
|
||||
public static String sendHttpRequest(String urlStr) throws Exception {
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setConnectTimeout(5000);
|
||||
conn.setReadTimeout(5000);
|
||||
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||
String line;
|
||||
StringBuilder result = new StringBuilder();
|
||||
while ((line = br.readLine()) != null) {
|
||||
result.append(line);
|
||||
}
|
||||
br.close();
|
||||
conn.disconnect();
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从地理编码结果中提取经纬度
|
||||
*/
|
||||
public static String extractLocation(String json) {
|
||||
int start = json.indexOf("\"location\":\"") + 12;
|
||||
int end = json.indexOf("\"", start);
|
||||
return json.substring(start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析并打印商家信息
|
||||
*/
|
||||
public static void parseAndPrint(String json) {
|
||||
String[] shops = json.split("\"name\":\"");
|
||||
for (int i = 1; i < shops.length; i++) {
|
||||
String item = shops[i];
|
||||
String name = getItem(item, "");
|
||||
String address = getItem(item, "address\":\"");
|
||||
String tel = getItem(item, "tel\":\"");
|
||||
String type = getItem(item, "type\":\"");
|
||||
String location = getItem(item, "location\":\"");
|
||||
|
||||
System.out.println("店名:" + name);
|
||||
System.out.println("地址:" + address);
|
||||
System.out.println("电话:" + tel);
|
||||
System.out.println("类型:" + type);
|
||||
System.out.println("坐标:" + location);
|
||||
System.out.println("---------------------------");
|
||||
}
|
||||
}
|
||||
|
||||
public static String getItem(String s, String pre) {
|
||||
try {
|
||||
int i = s.indexOf(pre) + pre.length();
|
||||
return s.substring(i, s.indexOf("\"", i));
|
||||
} catch (Exception e) {
|
||||
return "无";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user