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

您所在的位置:网站首页 高德地图上的房价 Python爬取链家网获取二手房数据并调用高德api获得经纬度

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

2024-07-13 22:50:36| 来源: 网络整理| 查看: 265

链家网获得数据地址,高德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)

简单看一下数据 数据 在这里插入图片描述



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭