失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Python爬取链家网获取二手房数据并调用高德api获得经纬度

Python爬取链家网获取二手房数据并调用高德api获得经纬度

时间:2022-10-26 07:08:57

相关推荐

Python爬取链家网获取二手房数据并调用高德api获得经纬度

链家网获得数据地址,高德api获得经纬度(同理链家网也可以换成其他58同城,赶集网的数据,因为反爬虫比较厉害,没时间整,就用链家网的数据先试试水)

首先爬链家网,Info包含一条信息

import jsonimport requestsfrom bs4 import BeautifulSoupimport re,sysfrom fake_useragent import UserAgentimport importlibimportlib.reload(sys)pro=['220.175.144.55:9999']ua = UserAgent()for i in range(1,2):# 循环构造urlurl = '/ershoufang/pg{}/'k = url.format(i)# 添加请求头,否则会被拒绝headers = {'Referer': '/ershoufang/','user-agent':ua.random}res = requests.get(k, headers=headers)# 基于正则表达式来解析网页内容,拿到所有的详情url# 原始可能是这么做的,但是后来发现bs4给我们提供了更方便的方法来取得各元素的内容# 正则表达式最重要的两个东西,.任意匹配字符,*匹配任意次数,?以html结束text = res.textre_set = pile('/ershoufang/[0-9]*.?html')re_get = re.findall(re_set,text)#去重lst2 = {}.fromkeys(re_get).keys()for name in lst2:res = requests.get(name, headers=headers)info = {}text2 = res.textsoup = BeautifulSoup(text2, 'html.parser')info['地址'] = soup.select('.main')[0].textinfo['总价'] = soup.select('.total')[0].textinfo['每平方售价'] = soup.select('.unitPriceValue')[0].textinfo['小区名称'] = soup.select('.info')[0].textinfo['所在区域'] = soup.select('.info a')[0].text + ':' + soup.select('.info a')[1].text

然后调用高德api,你肯定要申请key,从而使用高德的服务

进入高德开发官网,注册账号啥的

然后创建应用

创建成功后就能得到一个key,名称随意

然后获取完数据,大致是这样的

取红框数据获取经纬度

# 根据地址获取对应经纬度,通过高德地图的api接口来进行mc = soup.select('.info')[0].textlocation1 = '杭州' + mc# print(location1)base = '/v3/geocode/geo?key=填写你自己创建的Key&address='+location1response = requests.get(base)result = json.loads(response.text)info['经纬度']=result['geocodes'][0]['location']print(info)with open('G:/新建文件夹/pc/image/a.csv', 'a', encoding='utf-8')as data:print(str(info), file=data)

下面是全部代码,比较简单就不写函数封装了

import jsonimport requestsfrom bs4 import BeautifulSoupimport re,sysfrom fake_useragent import UserAgentimport importlibimportlib.reload(sys)pro=['220.175.144.55:9999']ua = UserAgent()for i in range(1,2):# 循环构造urlurl = '/ershoufang/pg{}/'k = url.format(i)# 添加请求头,否则会被拒绝headers = {'Referer': '/ershoufang/','user-agent':ua.random}res = requests.get(k, headers=headers)# 基于正则表达式来解析网页内容,拿到所有的详情url# 原始可能是这么做的,但是后来发现bs4给我们提供了更方便的方法来取得各元素的内容# 正则表达式最重要的两个东西,.任意匹配字符,*匹配任意次数,?以html结束text = res.textre_set = pile('/ershoufang/[0-9]*.?html')re_get = re.findall(re_set,text)#去重lst2 = {}.fromkeys(re_get).keys()for name in lst2:res = requests.get(name, headers=headers)info = {}text2 = res.textsoup = BeautifulSoup(text2, 'html.parser')info['地址'] = soup.select('.main')[0].textinfo['总价'] = soup.select('.total')[0].textinfo['每平方售价'] = soup.select('.unitPriceValue')[0].textinfo['小区名称'] = soup.select('.info')[0].textinfo['所在区域'] = soup.select('.info a')[0].text + ':' + soup.select('.info a')[1].text# 根据地址获取对应经纬度,通过高德地图的api接口来进行mc = soup.select('.info')[0].textlocation1 = '杭州' + mc# print(location1)base = '/v3/geocode/geo?key=填写你自己创建的key&address='+location1response = requests.get(base)result = json.loads(response.text)info['经纬度']=result['geocodes'][0]['location']print(info)with open('G:/新建文件夹/pc/image/a.csv', 'a', encoding='utf-8')as data:print(str(info), file=data)

简单看一下数据

如果觉得《Python爬取链家网获取二手房数据并调用高德api获得经纬度》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。