Python uses Baidu Map API to get the latitude and longitude of address in bulk

Source: Internet
Author: User

1.pip Install the Xlrd,xlwt,requests module.

2. Place the Excel file at the project directory.

Python code:

#coding: Utf-8import xlrdimport xlwtimport requestsimport urllibimport mathimport repattern_x = re.compile (R ' "x":(". +?") pattern_y = Re.compile (R ' "Y":(". +?") Def mercator2wgs84 (Mercator): # Key1=mercator.keys () [0] # Key2=mercator.keys () [1] point_x = mercator[0] Point_ y = mercator[1] x = point_x/20037508.3427892 * [+] y = point_y/20037508.3427892 * + + y = 180/math.pi * (2 * Math.atan (Math.exp (y * math.pi/180))-MATH.PI/2) return (x, y) def get_mercator (addr): Quote_addr = urllib.quo    Te (Addr.encode (' UTF8 ')) city = urllib.quote (U ' Lanzhou City '. Encode (' UTF8 ')) province = urllib.quote (U ' Gansu Province '. Encode (' UTF8 '))    If Quote_addr.startswith (city) or Quote_addr.startswith (province): Pass else:quote_addr = quote_addr s = urllib.quote (U ' Beijing ' encode (' UTF8 ')) api_addr = "http://api.map.baidu.com/?qt=gc&wd=%s&cn=%s&ie= utf-8&oue=1&fromproduct=jsapi&res=api&callback=bmap._rd._cbk62300 "% (quote_addr, s) req= Requests.get (api_addr) content = req.content x = Re.findall (pattern_x, content) y = Re.findall (pattern_y, conte         NT) If x:x = x[0] y = y[0] x = x[1:-1] y = y[1:-1] x = float (x) y = float (y) Location = (x, y) else:location = () return locationdef run (): data = Xlrd.open_workbook (' book2.x LSx ') rtable = data.sheets () [0] nrows = rtable.nrows values = rtable.col_values (0) workbook = xlwt. Workbook () wtable = Workbook.add_sheet (' data ', cell_overwrite_ok=true) row = 0 for value in Values:mercat or = Get_mercator (value) If Mercator:wgs = Mercator2wgs84 (mercator) Else:wgs = (' Not Found ', ' NotFound ') print "%s,%s,%s"% (value, wgs[0], wgs[1]) wtable.write (row, 0, value) wtable.wri Te (row, 1, wgs[0]) wtable.write (row, 2, wgs[1]) row = row + 1 workbook.save (' Data.xls ') if __name__ = = ' __ Main__ ': Run ()

Run to get the Data.xls result file.

Python uses Baidu Map API to get the latitude and longitude of address in bulk

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.