Python爬取链家网获取二手房数据并调用高德api获得经纬度 |
您所在的位置:网站首页 › 高德地图上的房价 › Python爬取链家网获取二手房数据并调用高德api获得经纬度 |
链家网获得数据地址,高德api获得经纬度(同理链家网也可以换成其他58同城,赶集网的数据,因为反爬虫比较厉害,没时间整,就用链家网的数据先试试水)
首先爬链家网,Info包含一条信息
import json
import requests
from bs4 import BeautifulSoup
import re,sys
from fake_useragent import UserAgent
import importlib
importlib.reload(sys)
pro=['220.175.144.55:9999']
ua = UserAgent()
for i in range(1,2):
# 循环构造url
url = 'http://hz.lianjia.com/ershoufang/pg{}/'
k = url.format(i)
# 添加请求头,否则会被拒绝
headers = {'Referer': 'https://hz.lianjia.com/ershoufang/',
'user-agent':ua.random}
res = requests.get(k, headers=headers)
# 基于正则表达式来解析网页内容,拿到所有的详情url
# 原始可能是这么做的,但是后来发现bs4给我们提供了更方便的方法来取得各元素的内容
# 正则表达式最重要的两个东西,.任意匹配字符,*匹配任意次数,?以html结束
text = res.text
re_set = re.compile('https://hz.lianjia.com/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.text
soup = BeautifulSoup(text2, 'html.parser')
info['地址'] = soup.select('.main')[0].text
info['总价'] = soup.select('.total')[0].text
info['每平方售价'] = soup.select('.unitPriceValue')[0].text
info['小区名称'] = soup.select('.info')[0].text
info['所在区域'] = soup.select('.info a')[0].text + ':' + soup.select('.info a')[1].text
然后调用高德api,你肯定要申请key,从而使用高德的服务
进入高德开发官网,注册账号啥的 然后创建应用 创建成功后就能得到一个key,名称随意 然后获取完数据,大致是这样的 取红框数据获取经纬度 # 根据地址获取对应经纬度,通过高德地图的api接口来进行 mc = soup.select('.info')[0].text location1 = '杭州' + mc # print(location1) base = 'https://restapi.amap.com/v3/geocode/geo?key=填写你自己创建的Key&address='+location1 response = 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 json import requests from bs4 import BeautifulSoup import re,sys from fake_useragent import UserAgent import importlib importlib.reload(sys) pro=['220.175.144.55:9999'] ua = UserAgent() for i in range(1,2): # 循环构造url url = 'http://hz.lianjia.com/ershoufang/pg{}/' k = url.format(i) # 添加请求头,否则会被拒绝 headers = {'Referer': 'https://hz.lianjia.com/ershoufang/', 'user-agent':ua.random} res = requests.get(k, headers=headers) # 基于正则表达式来解析网页内容,拿到所有的详情url # 原始可能是这么做的,但是后来发现bs4给我们提供了更方便的方法来取得各元素的内容 # 正则表达式最重要的两个东西,.任意匹配字符,*匹配任意次数,?以html结束 text = res.text re_set = re.compile('https://hz.lianjia.com/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.text soup = BeautifulSoup(text2, 'html.parser') info['地址'] = soup.select('.main')[0].text info['总价'] = soup.select('.total')[0].text info['每平方售价'] = soup.select('.unitPriceValue')[0].text info['小区名称'] = soup.select('.info')[0].text info['所在区域'] = soup.select('.info a')[0].text + ':' + soup.select('.info a')[1].text # 根据地址获取对应经纬度,通过高德地图的api接口来进行 mc = soup.select('.info')[0].text location1 = '杭州' + mc # print(location1) base = 'https://restapi.amap.com/v3/geocode/geo?key=填写你自己创建的key&address='+location1 response = 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)简单看一下数据 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |