python爬虫代码房-python爬虫爬取链家二手房信息
#coding=utf-8
import requests
from fake_useragent import UserAgent
from bs4 import BeautifulSoup
import json
import csv
import time
# 构建请求头
userAgent = UserAgent()
headers = {
'user-agent': userAgent .Chrome
}
# 声明一个列表存储字典
data_list = []
def start_spider(page):
#设置重连次数
requests.adapters.DEFAULT_RETRIES = 15
s = requests.session()
#设置连接活跃状态为False
s.keep_alive = False
#爬取的url,默认爬取的南京的链家房产信息
url = 'https://nj.lianjia.com/ershoufang/pg{}/'.format(page)
# 请求url
resp = requests.get(url, headers=headers,timeout=10)
# 讲返回体转换成Beautiful
soup = BeautifulSoup(resp.content, 'lxml')
# 筛选全部的li标签
sellListContent = soup.select('.sellListContent li.LOGCLICKDATA')
# 循环遍历
for sell in sellListContent:
try:
# 标题
title = sell.select('div.title a')[0].string
# 先抓取全部的div信息,再针对每一条进行提取
houseInfo = list(sell.select('div.houseInfo')[0].stripped_strings)
# 楼盘名字
loupan = houseInfo[0]
#对楼盘的信息进行分割
info = houseInfo[0].split('|')
# 房子类型
house_type = info[1].strip()
# 面积大小
area = info[2].strip()
# 房间朝向
toward = info[3].strip()
# 装修类型
renovation = info[4].strip()
# 房屋地址
positionInfo = ''.join(list(sell.select('div.positionInfo')[0].stripped_strings))
# 房屋总价
totalPrice = ''.join(list(sell.select('div.totalPrice')[0].stripped_strings))
# 房屋单价
unitPrice = list(sell.select('div.unitPrice')[0].stripped_strings)[0]
# 声明一个字典存储数据
data_dict = {}
data_dict['title'] = title
data_dict['loupan'] = loupan
data_dict['house_type'] = house_type
data_dict['area'] = area
data_dict['toward'] = toward
data_dict['renovation'] = renovation
data_dict['positionInfo'] = positionInfo
data_dict['totalPrice'] = totalPrice
data_dict['unitPrice'] = unitPrice
data_list.append(data_dict)
except Exception as e:
print(e)
continue
def main():
# 只爬取10页
for page in range(1, 10):
start_spider(page)
time.sleep(3)
# 将数据写入json文件
with open('data_json.json', 'a+', encoding='utf-8') as f:
json.dump(data_list, f, ensure_ascii=False, indent=4)
print('json文件写入完成')
# 将数据写入csv文件
with open('./data_csv.csv', 'w', encoding='utf-8', newline='') as f:
# 表头
print(data_list)
title = data_list[0].keys()
# 创建writer对象
writer = csv.DictWriter(f, title)
# 写入表头
writer.writeheader()
# 批量写入数据
writer.writerows(data_list)
print('csv文件写入完成')
if __name__ == '__main__':
main()
python爬虫代码房-python爬虫爬取链家二手房信息相关推荐
- python关于二手房的课程论文_基于python爬取链家二手房信息代码示例
基本环境配置 python 3.6 pycharm requests parsel time 相关模块pip安装即可 确定目标网页数据 哦豁,这个价格..................看到都觉得脑阔 ...
- python爬虫requests源码链家_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 ...
- python爬取链家二手房信息
爬取过程分析: 1.链家网url:https://bj.lianjia.com/ershoufang/pg2/ 显示的二手房信息每个url递增一 2.访问时要加头信息,否则无法访问 3.用beauti ...
- python链家二手房_python 爬取链家二手房信息
'''Created on 2017-10-9 @author: wbhuangzhiqiang''' importsysimportreimportcsvimporturllib.requestfr ...
- 掌财社:python怎么爬取链家二手房的数据?爬虫实战!
我们知道爬虫的比较常见的应用都是应用在数据分析上,爬虫作为数据分析的前驱,它负责数据的收集.今天我们以python爬取链家二手房数据为例来进行一个python爬虫实战.(内附python爬虫源代码) ...
- python爬虫requests源码链家_python爬虫——爬取链家房价信息(未完待续)
爬取链家房价信息(未完待续) items.py # -*- coding: utf-8 -*- # Define here the models for your scraped items # # ...
- 租房不入坑不进坑,Python爬取链家二手房的数据,提前了解租房信息
目录 前言 一.查找数据所在位置: 二.确定数据存放位置: 三.获取html数据: 四.解析html,提取有用数据: 前言 贫穷限制了我的想象,从大学进入到社会这么久,从刚开始的兴致勃勃,觉得钱有什么 ...
- python-scrapy-MongoDB 爬取链家二手房
python-scrapy-MongoDB 爬取链家二手房 链家二手房房源数据抓取 目标网址为http://bj.lianjia.com/ershoufang/ 分析网址 创建项目 scrapy st ...
最新文章
- Simple Transformer:用BERT、RoBERTa、XLNet、XLM和DistilBERT进行多类文本分类
- 强化学习最新作品:谷歌最新思想、MIT新书推荐、Sutton经典之作!
- iframe解决跨域ajax请求的方法
- zabbix监控mysql最简单的方法
- FreeRTOS 中断优先级嵌套错误引发HardFault异常解决
- cassandra的命令
- 融合通信常见问题3月刊 | 云信小课堂
- VGG19续读【精细】,为什么叫做VGG19?==>【每一层可以看做是很多个局部特征的提取器,可以用作局部特征提取】
- js 禁止android手机返回键,js禁止浏览器,微信,及手机物理键的返回
- Python 3.10新增软关键字实现真正意义上的多分支选择结构
- sql server版本号_识别SQL Server版本号的不同方法
- “Vince on iPhone”开张了
- 通过anaconda安装jupyter lab
- SQL:postgresql中拼接字符串
- 新题速看~2021阿里、腾讯、字节都在问的SQL数据库笔试题及答案都给你整理好啦
- psim扰动观察法编程c语言,一种数字PID控制扰动观察法光伏电池MPPT仿真.doc
- 【转•科普】为什么Nikola Tesla是迄今为止最伟大的极客(爱迪生啊爱迪生)
- 中科大一所学校撑起中国人工智能半壁江山
- 输入法的新时代:搜狗、讯飞、百度鏖战智能语音
- 【编程开发】之 Java 实现邮件发送