R实例:批量抓取位置经纬度坐标

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

抓取高德地图经纬度坐标:

library(rvest)
library(XML)
housedata<-list()
houselist<-c("百业沣尚|塱沙三路","柏丽花园|南桂西路34号","翠堤明珠|东平路","翠景台|汾江中路","翠影华庭|轻工南五街","永乐花苑|木棉街9号","优悦城|百灵路90号","瑜翠园|高明大道中88号","御翠居|永安街","御江名苑|高明大道东636号","御景花园|永华街8号")
a<-length(houselist)
for(n in 1:a)
	{
	i<-houselist[n]
	url<-paste("http://restapi.amap.com/v3/geocode/geo?key=25eebfbe4cb0465446df0c538be5fb49&address=",i,"&city=佛山",sep="")
	web<-read_html(url)
	xmldoc<-xmlRoot(xmlParse(web,encoding="UTF-8"))
	rootnode<-xmlRoot(xmldoc)
	res<-xmlValue(rootnode)
	res2<-strsplit(res,"location")[[1]][2]
	GIS<-strsplit(res2,'"')[[1]][3]
	data<-data.frame(name=i,GIS)
	housedata[[n]]<-data
	next
	}
final<-do.call(rbind,housedata)
print(final)
                       name                  GIS
1         百业沣尚|塱沙三路 113.129409,23.012523
2     柏丽花园|南桂西路34号 113.134891,23.032580
3           翠堤明珠|东平路 113.116212,22.976987
4           翠景台|汾江中路 113.108540,23.041478
5       翠影华庭|轻工南五街 113.095901,23.029839
6        永乐花苑|木棉街9号 112.795954,22.819140
7         优悦城|百灵路90号 112.836526,22.885311
8     瑜翠园|高明大道中88号 112.786129,22.864950
9             御翠居|永安街 112.878767,22.897826
10 御江名苑|高明大道东636号 112.849972,22.893118
11       御景花园|永华街8号 113.271870,22.840160

抓取百度地图经纬度坐标:

library(rvest)
library(XML)
housedata<-list()
houselist<-c("百业沣尚|塱沙三路","柏丽花园|南桂西路34号","翠堤明珠|东平路","翠景台|汾江中路","翠影华庭|轻工南五街","永乐花苑|木棉街9号","优悦城|百灵路90号","瑜翠园|高明大道中88号","御翠居|永安街","御江名苑|高明大道东636号","御景花园|永华街8号")
a<-length(houselist)
for(n in 1:a)
	{
	i<-houselist[n]
	url<-paste("http://api.map.baidu.com/geocoder/v2/?callback=renderOption&output=json&address=",i,"&city=佛山市&ak=Sdt5HFwA96eZ5k8x5gURUlEwK3XTfjIr",sep="")
	web<-read_html(url)
	xmldoc<-xmlRoot(xmlParse(web,encoding="UTF-8"))
	rootnode<-xmlRoot(xmldoc)
	res<-xmlValue(rootnode)
	res2<-strsplit(res,"location")[[1]][2]
	GIS<-strsplit(res2,'"')[[1]][c(4,6)]
	lon<-sub(",","",sub(":","",strsplit(GIS," ")[1]))
	lat<-sub(",","",sub(":","",sub("}","",strsplit(GIS," ")[2])))
	data<-data.frame(name=i,lon,lat)
	housedata[[n]]<-data
	next
	}
final<-do.call(rbind,housedata)
print(final)
                       name                lon                lat
1         百业沣尚|塱沙三路 113.10242704020868 23.046192775504044
2     柏丽花园|南桂西路34号  113.1417733739906 23.038383425860347
3           翠堤明珠|东平路 113.12300901245327 22.982707729631703
4           翠景台|汾江中路  113.1156260999778  23.04727250520667
5       翠影华庭|轻工南五街 113.10203762477024  23.03595823786657
6        永乐花苑|木棉街9号 112.80252940317527 22.825304613314509
7         优悦城|百灵路90号 112.84567043624964 22.891755194930789
8     瑜翠园|高明大道中88号 112.79256383181708  22.87108187870626
9             御翠居|永安街 112.88520127399433 22.903341184068905
10 御江名苑|高明大道东636号 112.85649100770132  22.89856531763583
11       御景花园|永华街8号 112.88354869638208 22.902318484835566

转载于:https://my.oschina.net/u/3093769/blog/995499

你可能感兴趣的:(R实例:批量抓取位置经纬度坐标)