地址转换成百度经纬度,数据太多!不愿意花钱?

需求

最近出了一个这样的需求,需要把几百万的数据,进行地址,进行经纬度转换

百度api

缺点:需要收费,然后看了下价格,有点贵,不愿意花钱?那没就,只能自己进行数据爬取

先找好目标网站

https://apis.map.qq.com/jsapi?qt=geoc&addr=
没错就他家了,直接拼接就能直接反显,最后得到JSON解析得到数据

技术要点

伪造IP
解析JSON
HTTP请求

开发包

commons-codec-1.9.jar
commons-httpclient-3.1.jar
commons-logging-1.1.1.jar
json-2.2.1.jar
json-lib-2.4-jdk15.jar
json-parser_fat.jar
源码下载地址:
https://download.csdn.net/download/qq_15294055/10850953
作为一个程序员,你怎摸那么多废话,老铁们上代码了

package com.bx.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import net.sf.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.Random;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class InitialUtil {
	public static final String STR_F = "f";
	public static final String STR_T = "t";
	private static final String ak = "";// 百度地图经纬度反现密钥

	/** 查询百度接口地址转经�? */
	public static String loadJSON(String url) {
		StringBuilder json = new StringBuilder();
		try {
			URL oracle = new URL(url);
			URLConnection yc = oracle.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
			String inputLine = null;
			while ((inputLine = in.readLine()) != null) {
				json.append(inputLine);
			}
			in.close();
		} catch (MalformedURLException e) {
		} catch (IOException e) {
		}
		return json.toString();
	}

	/**
	 * 获取百度接口地址转经纬度
	 */
	public static String getLngAndLat(String address) {
		String json = "";
		try {
			String url = "http://api.map.baidu.com/geocoder/v2/?address=" + address + "&city=北京�?&output=json&ak=" + ak;
			json = loadJSON(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}

	/**
	 * 获取百度地图的经纬度
	 * 
	 * @param address
	 */
	public static String getcoorder(String address) {
		// https://jingweidu.51240.com/
		HttpClient httpClient = new HttpClient();
		try {
			String urlString = "https://apis.map.qq.com/jsapi?qt=geoc&addr=" + getURLEncoderString(address);
			GetMethod post = new GetMethod(urlString);
			String randomIp = getRandomIp();
			post.setRequestHeader("X-Real-IP", randomIp);
			post.setRequestHeader("X-Forwarded-For", randomIp);
			post.setRequestHeader("Proxy-Client-IP", randomIp);
			post.setRequestHeader("WL-Proxy-Client-IP", randomIp);
			post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8"); // 设置编码
			httpClient.executeMethod(post);
			// System.out.println(post.getResponseBodyAsString());
			JSONObject json = JSONObject.fromObject(post.getResponseBodyAsString());
			JSONObject jsono = json.getJSONObject("detail");
			System.out.println(json);
			return map_tx2bd(Double.valueOf(jsono.get("pointx").toString()), Double.valueOf(jsono.get("pointy").toString()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 坐标转换,腾讯地图转换成百度地图坐标
	 * 
	 * @param lat
	 *            腾讯纬度
	 * @param lon
	 *            腾讯经度
	 * @return 返回结果:经度,纬度
	 */
	public static String map_tx2bd(double lon, double lat) {
		double bd_lat;// 纬度
		double bd_lon;// 经度
		double x_pi = 3.14159265358979324;
		double x = lon, y = lat;
		double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
		double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
		bd_lon = z * Math.cos(theta) + 0.0065;
		bd_lat = z * Math.sin(theta) + 0.006;
		return bd_lon + "," + bd_lat;
	}

	/**
	 * 坐标转换,百度地图坐标转换成腾讯地图坐标
	 * 
	 * @param lat
	 *            百度坐标纬度
	 * @param lon
	 *            百度坐标经度
	 * @return 返回结果:经度,纬度
	 */
	public String map_bd2tx(double lat, double lon) {
		double tx_lat;
		double tx_lon;
		double x_pi = 3.14159265358979324;
		double x = lon - 0.0065, y = lat - 0.006;
		double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
		double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
		tx_lon = z * Math.cos(theta);
		tx_lat = z * Math.sin(theta);
		return tx_lat + "," + tx_lon;
	}

	private final static String ENCODE = "utf-8";

	/**
	 * URL 转码
	 * 
	 * @return String
	 * @date 2015-3-17 下午04:10:28
	 */
	public static String getURLEncoderString(String str) {
		String result = "";
		if (null == str) {
			return "";
		}
		try {
			result = java.net.URLEncoder.encode(str, ENCODE);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 生成随机ip数据
	 * 
	 * @return
	 */
	public static String getRandomIp() {
		// ip范围
		int[][] range = { { 607649792, 608174079 }, // 36.56.0.0-36.63.255.255
				{ 1038614528, 1039007743 }, // 61.232.0.0-61.237.255.255
				{ 1783627776, 1784676351 }, // 106.80.0.0-106.95.255.255
				{ 2035023872, 2035154943 }, // 121.76.0.0-121.77.255.255
				{ 2078801920, 2079064063 }, // 123.232.0.0-123.235.255.255
				{ -1950089216, -1948778497 }, // 139.196.0.0-139.215.255.255
				{ -1425539072, -1425014785 }, // 171.8.0.0-171.15.255.255
				{ -1236271104, -1235419137 }, // 182.80.0.0-182.92.255.255
				{ -770113536, -768606209 }, // 210.25.0.0-210.47.255.255
				{ -569376768, -564133889 }, // 222.16.0.0-222.95.255.255
		};

		Random rdint = new Random();
		int index = rdint.nextInt(10);
		String ip = num2ip(range[index][0] + new Random().nextInt(range[index][1] - range[index][0]));
		return ip;
	}

	/*
	 * 将十进制转换成IP地址
	 */
	public static String num2ip(int ip) {
		int[] b = new int[4];
		String x = "";
		b[0] = (int) ((ip >> 24) & 0xff);
		b[1] = (int) ((ip >> 16) & 0xff);
		b[2] = (int) ((ip >> 8) & 0xff);
		b[3] = (int) (ip & 0xff);
		x = Integer.toString(b[0]) + "." + Integer.toString(b[1]) + "." + Integer.toString(b[2]) + "." + Integer.toString(b[3]);
		return x;
	}

	// 测试方法
	public static void main(String[] args) {
		// https://jingweidu.51240.com/

		// String string = InitialUtil.getcoorder("北京市海�?区有福敬老院");
		// System.out.println(string);

		HttpClient httpClient = new HttpClient();
		for (int i = 0; i < 1; i++) {
			try {
				String address = getURLEncoderString("北京市海�?区有福敬老院");
				String urlString = "https://apis.map.qq.com/jsapi?qt=geoc&addr=" + address;
				GetMethod post = new GetMethod(urlString);
				String randomIp = getRandomIp();
				post.setRequestHeader("X-Real-IP", randomIp);
				post.setRequestHeader("X-Forwarded-For", randomIp);
				post.setRequestHeader("Proxy-Client-IP", randomIp);
				post.setRequestHeader("WL-Proxy-Client-IP", randomIp);
				post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8"); // 设置编码
				httpClient.executeMethod(post);
				// System.out.println(post.getResponseBodyAsString());
				JSONObject json = JSONObject.fromObject(post.getResponseBodyAsString());// new
																						// JSONObject();
				JSONObject jsono = json.getJSONObject("detail");
				System.out.println(json);
				System.out.println("解析出来的经度:" + String.format("%.7f", Double.parseDouble(jsono.get("pointx").toString())));
				System.out.println("解析出来的纬度:" + String.format("%.7f", Double.parseDouble(jsono.get("pointy").toString())));
				System.out.println("转换成百度经纬度�?" + map_tx2bd(Double.valueOf(jsono.get("pointx").toString()), Double.valueOf(jsono.get("pointy").toString())));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我们可以使用Python编程语言结合高德API来实现将Excel表中的地址批量转换为高德经纬度。 首先,我们需要安装pandas和requests库来处理Excel表和发送API请求。可以使用以下命令安装这些库: ```python pip install pandas requests ``` 然后,我们需要从Excel中读取地址数据并进行转换。可以使用以下代码来实现: ```python import pandas as pd import requests # 读取Excel文件 data = pd.read_excel('地址表.xlsx') # 遍历每一行数据 for index, row in data.iterrows(): address = row['地址'] # 发送地址转换请求 url = 'https://restapi.amap.com/v3/geocode/geo' params = {'key': '你的高德API密钥', 'address': address} response = requests.get(url, params=params) # 解析响应并获取经纬度信息 result = response.json() if result['status'] == '1' and int(result['count']) >= 1: geocode = result['geocodes'][0] location = geocode['location'] # 更新Excel表中的经纬度字段 data.at[index, '经纬度'] = location # 保存修改后的Excel文件 data.to_excel('经纬度表.xlsx', index=False) ``` 在上面的代码中,我们首先使用pandas库的`read_excel`函数读取Excel文件,并使用`iterrows`方法遍历每一行数据。然后,我们将地址数据传递给高德API的`geocode/geo`接口,并将响应中的经纬度信息更新到Excel表中的相应字段。最后,我们使用pandas的`to_excel`方法保存修改后的Excel文件。 需要注意的是,你需要将代码中的`你的高德API密钥`替换为你自己的高德API密钥,以便能够功发送地址转换请求。此外,你还需要将代码中的`地址表.xlsx`替换为你自己的Excel文件名,以及将`经纬度表.xlsx`替换为你希望保存经纬度数据的Excel文件名。 希望以上信息能够帮助你使用Python和高德API批量将Excel表中的地址转换为高德经纬度

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值