利用requests库和BeautifulSoup库爬取链家二手房的基本数据
文章目录
- 内容简介
- 一、在写代码前我们应该做的事
- 二、步骤
- 1.安装、引入所需要的库
- 2.解析网页
- 总结
内容简介
利用requests库和BeautifulSoup库爬取链家二手房的基本数据。
一、在写代码前我们应该做的事
你知道吗?写代码虽说是很重要但它其实并不是最重要的,再写代码前,我们应该先理好思路。我究竟要干嘛?我想要获取什么样的数据?哪些数据对我来说才是我想要的?
比如说,拿今天爬取链家二手房数据来举个例子。首先我们先进入链家二手房的官网:
我们可以在官网上看到这样的一个个的界面,此时我们可以在这一个个的界面上获取到我们圈绿的一些数据,当然如果你觉得这些数据不够,你可以点进这个界面,去看看它的二级页面还有些什么我们可以用到的。接下来,我们一起去看看它的二级页面:
在这里我们会发现圈红的两处数字是一样的,而且每一个二级页面所对应的编号都不相同,所以在这里我们可以将编号爬取下来,当作每一个二手房的唯一数据。
下面的这些数据是可以根据我们的需要来爬取的内容:
二、步骤
1.安装、引入所需要的库
工欲善其事,必先利其器。本次爬取二手房数据,我们要用到requests库和BeautifulSoup库
安装库的方法有多种这里我们就只是说其中一种:以管理员的身份打开cmd,输入以下内容
在pycharm中导入数据库
import requests
from bs4 import BeautifulSoup
2.解析网页
先获取网页的内容代码:因为有很多网站都有反爬虫,所以我们需要设置一个head,以此来跳过反爬虫。
headDict = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36 Edg/90.0.818.42"
}def downWebContent(url):r = requests.get(url, headers = headDict)r.encoding = r.apparent_encodingprint(r.status_code)return r.text
经过我们查看网页得知,在二手房表面只有三十个房子的数据,这和前面写的房子总数是相差很远很远的,后面才发现别人是将其分页了的,所以我们在这里需要设置一个循环将所有数据爬取下来。
代码:
'''下载全部列表数据'''def down():for i in range(1, 31):strurl = "https://cd.lianjia.com/ershoufang/pg" + str(i) + "rs%E6%88%90%E9%83%BD/"content = downWebContent(strurl)parseHouseList(content)
当获得所有房子的网页之后,我们就需要去分析网页结构,以此来找到我们所需要的数据。在二手房界面按下“F12”键,然后点击Elements,按下“F5”刷新。
代码:
'''解析二手房列表'''def parseHouseList(webContent):soup = BeautifulSoup(webContent,"html.parser")ultags = soup.find("ul",class_="sellListContent")litags = ultags.find_all("li")for litag in litags:image = litag.find("a",class_="noresultRecommend img LOGCLICKDATA")["href"]content = downWebContent(image)parseHouseDetail(content)
我们刚刚说了只是一级页面的数据是不够的,我们需要进入它的详情面,去获取其他的信息。经过我们分析知道有些房子的基本属性并不全,所以在这里我们设置了一个循环,当基本属性不全时,我们就跳过它。代码:
'''解析二手房详情'''def parseHouseDetail(webContent):soup = BeautifulSoup(webContent,"html.parser")contentList = []div2 = soup.find("div", class_="overview")contentTag = div2.find("div",class_="content")mContentTag = soup.find("div", class_="m-content")# 基本属性ulTag = mContentTag.find("div", class_="base").find("div", class_="content").ullis = ulTag.find_all("li")if len(lis) == 11:q = lis[0].getText().replace("房屋户型", "").replace("\n","")else:print("none")# 交易属性ulTag = mContentTag.find("div", class_="transaction").find("div", class_="content").ullis = ulTag.find_all("li")date = lis[0].getText().replace("挂牌时间", "").replace("\n","")
最后,当我们获取到了所有我们想要的东西之后,我们就可以把这些数据写在一个文本文档里面,方便我们查看。代码:
#文档打开方式
f = open("链家.txt", mode="a+", encoding="utf-8")#内容写入
contentList.append()#最后要将文档关闭
f.write(";".join(contentList) + "\n")
f.close()
完整代码:
import requests
from bs4 import BeautifulSoupheadDict = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36 Edg/90.0.818.42"
}def downWebContent(url):r = requests.get(url, headers = headDict)r.encoding = r.apparent_encodingprint(r.status_code)return r.text'''
下载全部列表数据
'''
def down():for i in range(1, 31):strurl = "https://cd.lianjia.com/ershoufang/pg" + str(i) + "rs%E6%88%90%E9%83%BD/"content = downWebContent(strurl)parseHouseList(content)'''
解析二手房列表
'''
def parseHouseList(webContent):soup = BeautifulSoup(webContent,"html.parser")ultags = soup.find("ul",class_="sellListContent")litags = ultags.find_all("li")for litag in litags:image = litag.find("a",class_="noresultRecommend img LOGCLICKDATA")["href"]content = downWebContent(image)parseHouseDetail(content)'''
解析二手房详情
'''
def parseHouseDetail(webContent):soup = BeautifulSoup(webContent,"html.parser")f = open("链家.txt", mode="a+", encoding="utf-8")contentList = []div2 = soup.find("div", class_="overview")contentTag = div2.find("div",class_="content")l = contentTag.find("span", class_="total").getText()j = contentTag.find("span", class_="unit").getText()price = l+jmianji = contentTag.find("div", class_="unitPrice").find("span",class_="unitPriceValue").getText()community = contentTag.find("div", class_="communityName").find("a", class_="info").getText()area = contentTag.find("div", class_="areaName").find("span", class_="info").find_all("a")[0].getText()town = contentTag.find("div", class_="areaName").find("span", class_="info").find_all("a")[1].getText()bianhao = contentTag.find("div", class_="houseRecord").getText().replace("链家", "").replace("举报", "")contentList.append(bianhao)contentList.append(area)contentList.append(town)contentList.append(community)contentList.append(mianji)contentList.append(price)mContentTag = soup.find("div", class_="m-content")# 基本属性ulTag = mContentTag.find("div", class_="base").find("div", class_="content").ullis = ulTag.find_all("li")if len(lis) == 11:q = lis[0].getText().replace("房屋户型", "").replace("\n","")contentList.append(q)w = lis[1].getText().replace("所在楼层", "").replace("\n","")contentList.append(w)e = lis[2].getText().replace("建筑面积", "").replace("\n","")contentList.append(e)r = lis[3].getText().replace("户型结构", "").replace("\n","")contentList.append(r)t = lis[4].getText().replace("套内面积", "").replace("\n","")contentList.append(t)y = lis[5].getText().replace("建筑类型", "").replace("\n","")contentList.append(y)u = lis[6].getText().replace("房屋朝向", "").replace("\n","")contentList.append(u)i = lis[7].getText().replace("建筑结构", "").replace("\n","")contentList.append(i)o = lis[8].getText().replace("装修情况", "").replace("\n","")contentList.append(o)p = lis[9].getText().replace("梯户比例", "").replace("\n","")contentList.append(p)l = lis[10].getText().replace("配备", "").replace("\n","")contentList.append(l)else:print("none")# 交易属性ulTag = mContentTag.find("div", class_="transaction").find("div", class_="content").ullis = ulTag.find_all("li")date = lis[0].getText().replace("挂牌时间", "").replace("\n","")quan = lis[1].getText().replace("交易权属", "").replace("\n", "")yongtu = lis[3].getText().replace("房屋用途", "").replace("\n", "")diyaimage = lis[6].getText().replace("抵押信息","").replace(" ","").replace("\n","")contentList.append(date)contentList.append(quan)contentList.append(yongtu)contentList.append(diyaimage)f.write(";".join(contentList) + "\n")f.close()if __name__ == "__main__":down()
总结:
当我们需要写代码的时候,第一步永远都不是马上开始写代码,而是先分析网页,看看我们需要什么,确定好了,再开始写代码,解析网页内容。
代码一般难的都是如何去解析网页,怎样才能找到我们需要的值。
利用requests库和BeautifulSoup库爬取链家二手房的基本数据相关推荐
- python-scrapy-MongoDB 爬取链家二手房
python-scrapy-MongoDB 爬取链家二手房 链家二手房房源数据抓取 目标网址为http://bj.lianjia.com/ershoufang/ 分析网址 创建项目 scrapy st ...
- 租房不入坑不进坑,Python爬取链家二手房的数据,提前了解租房信息
目录 前言 一.查找数据所在位置: 二.确定数据存放位置: 三.获取html数据: 四.解析html,提取有用数据: 前言 贫穷限制了我的想象,从大学进入到社会这么久,从刚开始的兴致勃勃,觉得钱有什么 ...
- 掌财社:python怎么爬取链家二手房的数据?爬虫实战!
我们知道爬虫的比较常见的应用都是应用在数据分析上,爬虫作为数据分析的前驱,它负责数据的收集.今天我们以python爬取链家二手房数据为例来进行一个python爬虫实战.(内附python爬虫源代码) ...
- python关于二手房的课程论文_基于python爬取链家二手房信息代码示例
基本环境配置 python 3.6 pycharm requests parsel time 相关模块pip安装即可 确定目标网页数据 哦豁,这个价格..................看到都觉得脑阔 ...
- Python 爬取链家二手房,我在北京买房的经历
本节所讲内容: 链家网站前期分析 利用requests爬取数据 爬取数据存储MongoDB(代码请看最后) 链家网站前期分析 今天我们主要对链家二手房数据爬取,看下我们目前的资金能买那一套.链家二手房 ...
- python爬虫requests源码链家_python爬虫爬取链家二手房信息
#coding=utf-8 import requests from fake_useragent import UserAgent from bs4 import BeautifulSoup imp ...
- python爬取链家二手房信息
爬取过程分析: 1.链家网url:https://bj.lianjia.com/ershoufang/pg2/ 显示的二手房信息每个url递增一 2.访问时要加头信息,否则无法访问 3.用beauti ...
- python爬虫代码房-python爬虫爬取链家二手房信息
#coding=utf-8 import requests from fake_useragent import UserAgent from bs4 import BeautifulSoup imp ...
- python二手房使用教程_python爬虫爬取链家二手房信息
#coding=utf-8 import requests from fake_useragent import UserAgent from bs4 import BeautifulSoup imp ...
- scrapy 爬取链家二手房数据
学习使用 只爬取展示的展示的3000条数据 spider: # -*- coding: utf-8 -*- from urllib import parse import scrapy from sc ...
最新文章
- 知识驱动的推荐系统:现状与展望
- Linux内核进程调度的时机和进程切换
- CentOS下开启mysql远程连接,远程管理数据库
- 「POJ 1135」Domino Effect(dfs)
- SegmentFault D-Day 北京:大数据
- 前端学习(1989)vue之电商管理系统电商系统之渲染商品列表数据
- 服务器部署多个tomcat方法
- Chrome浏览器获取XPATH的方法----通过开发者工具获取
- 乌班图服务器系统怎么配ip,Ubuntu server14-04 服务器双网卡怎么配置静态ip
- linux make项目管理器,Linux中makefile项目管理
- python解析json传入变量_Python 使用 docopt 解析json参数文件过程讲解
- mysql查binlog删除时间_mysql的binlog日志删除查询
- python多级目录import_深入理解Python中import机制
- 百度和bing的背景肤色图片的保存
- 调节RStudio的代码字体大小
- 在Python里安装Jieba中文分词组件
- 解压缩软件(winrar中文版)pjb 附使用说明
- 修改计算机ip 批处理,怎么使用批处理bat设置静态IP地址?
- python1~10阶乘_阶乘python怎么打
- 地图刷新 android,android – 如何刷新谷歌地图.