python爬取南京市房价_python 爬取链家南京的数据
spider代码
–– coding: utf-8 ––
import scrapy
from lxml import etree
import time
import random
import requests
from …items import LianjiananjingItem
class LianjiananjingSpider(scrapy.Spider):
name = ‘lianjiananjing’
allowed_domains = [‘nj.lianjia.com’]
start_urls = ‘https://nj.lianjia.com/ershoufang/’
def start_requests(self):
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:60.0) Gecko/20100101 Firefox/60.0'
headers = {'User-Agent': user_agent}
yield scrapy.Request(url=self.start_urls,headers=headers,method='GET',callback=self.parse)
def parse(self,response):
print(response)
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:60.0) Gecko/20100101 Firefox/60.0'
headers = {'User-Agent': user_agent}
lists = response.body.decode('utf-8')
selector = etree.HTML(lists)
# 在进行网页抓取的时候,分析定位html节点
# 将文件读入,解析成树,然后根据路径定位到每个节点
area_list = selector.xpath('/html/body/div[3]/div/div[1]/dl[2]/dd/div[1]/div/a')
#etree.HTML得到的内容可以直接使用xpath
for area in area_list:
try:
area_hanzi = area.xpath('text()').pop()
print(area_hanzi)
area_pinyin = area.xpath('@href').pop().split('/')[2] #['/ershoufang/pudong/', '/ershoufang/minhang/', '/ershoufang/baoshan/'...]
area_url = 'http://nj.lianjia.com/ershoufang/{}/'.format(area_pinyin)
print(area_url)
yield scrapy.Request(url=area_url, headers=headers, callback=self.detail_url, meta={"id1":area_hanzi, "id2":area_pinyin})
except Exception:
print('44444444444444')
def detail_url(self,response):
for i in range(1, 40): # 获取1-39页
url = 'http://nj.lianjia.com/ershoufang/{}/pg{}/'.format(response.meta["id2"], str(i))
time.sleep(random.randint(1, 5)) # 随机等待1-5秒
try:
contents = requests.get(url)
contents = etree.HTML(contents.content.decode('utf-8'))
houselist = contents.xpath('/html/body/div[4]/div[1]/ul/li')
for house in houselist:
try:
item = LianjiananjingItem()
item['page'] = i
item['title'] = house.xpath('div[1]/div[1]/a/text()').pop()
item['community'] = house.xpath('div[1]/div[2]/div/a/text()').pop()
item['model'] = house.xpath('div[1]/div[2]/div/text()').pop().split('|')[1]
item['area'] = house.xpath('div[1]/div[2]/div/text()').pop().split('|')[2]
item['focus_num'] = house.xpath('div[1]/div[4]/text()').pop().split('/')[0]
item['watch_num'] = house.xpath('div[1]/div[4]/text()').pop().split('/')[1]
item['time'] = house.xpath('div[1]/div[4]/text()').pop().split('/')[2]
item['price'] = house.xpath('div[1]/div[6]/div[1]/span/text()').pop()
item['average_price'] = house.xpath('div[1]/div[6]/div[2]/span/text()').pop()
item['link'] = house.xpath('div[1]/div[1]/a/@href').pop()
item['city'] = response.meta["id1"]
self.url_detail = house.xpath('div[1]/div[1]/a/@href').pop()
except Exception:
print('2222222')
print(item)
yield item
except Exception:
print('2222222')
#setting 要注意把
ROBOTSTXT_OBEY = False,因为带有htpps的请求会被禁
ITEM_PIPELINES = {
‘Lianjiananjing.pipelines.LianjiananjingPipeline’: 300,
}
#item代码
–– coding: utf-8 ––
Define here the models for your scraped items
See documentation in:
import scrapy
class LianjiananjingItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
page = scrapy.Field()
title = scrapy.Field()
community = scrapy.Field()
model = scrapy.Field()
area = scrapy.Field()
focus_num = scrapy.Field()
watch_num =scrapy.Field()
time = scrapy.Field()
price = scrapy.Field()
average_price =scrapy.Field()
link = scrapy.Field()
city = scrapy.Field()
#pipelines代码
import csv
class LianjiananjingPipeline(object):
def process_item(self, item, spider):
f = open(‘V:/Python/scapyProject/Lianjiananjing/lianjiananjing.csv’, ‘a+’)
write = csv.writer(f)
write.writerow((item[‘title’], item[‘community’], item[‘model’], item[‘area’],
item[‘focus_num’], item[‘watch_num’], item[‘time’], item[‘price’], item[‘average_price’],
item[‘link’],
item[‘city’], item[‘page’]))
return item
#main 用于调试等
#!/usr/bin/env python
#-– coding:utf-8 ––
from scrapy.cmdline import execute
import os
import sys
#添加当前项目的绝对地址
sys.path.append(os.path.dirname(os.path.abspath(file)))
#执行 scrapy 内置的函数方法execute, 使用 crawl 爬取并调试,最后一个参数jobbole 是我的爬虫文件名
execute([‘scrapy’, ‘crawl’, ‘lianjiananjing’])
python爬取南京市房价_python 爬取链家南京的数据相关推荐
- python爬取南京市房价_python 爬取宁波市房价热力图
爬取安居客的宁波市二手房的平均价格 下面是python代码: from selenium import webdriver import bs4 from time import sleep impo ...
- python爬取南京市房价_Python的scrapy之爬取链家网房价信息并保存到本地
因为有在北京租房的打算,于是上网浏览了一下链家网站的房价,想将他们爬取下来,并保存到本地. 先看链家网的源码..房价信息 都保存在 ul 下的li 里面 爬虫结构: 其中封装了一个数据库处理模 ...
- 租房不入坑不进坑,Python爬取链家二手房的数据,提前了解租房信息
目录 前言 一.查找数据所在位置: 二.确定数据存放位置: 三.获取html数据: 四.解析html,提取有用数据: 前言 贫穷限制了我的想象,从大学进入到社会这么久,从刚开始的兴致勃勃,觉得钱有什么 ...
- 掌财社:python怎么爬取链家二手房的数据?爬虫实战!
我们知道爬虫的比较常见的应用都是应用在数据分析上,爬虫作为数据分析的前驱,它负责数据的收集.今天我们以python爬取链家二手房数据为例来进行一个python爬虫实战.(内附python爬虫源代码) ...
- 利用xpath爬取链家租房房源数据并利用pandas保存到Excel文件中
我们的需求是利用xpath爬取链家租房房源数据,并将数据通过pandas保存到Excel文件当中 下面我们看一下链家官网的房源信息(以北京为例) 如图所示,我们通过筛选得到北京租房信息 那么我们需要将 ...
- 爬取链家网二手房数据并保存到mongodb中
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 爬取链家网二手房数据并保存到mongodb中 文章目录 前言 一.爬虫的介绍 二.协程的介绍 三.css选择器 四.基于asyncio ...
- Python:爬了下链家的小区数据,为数据分析弄素材~
前言:最近在学数据分析,包括主成分分析.因子分析.聚类...等等,没有数据可不行啊,所以就把链家的小区数据给爬了一下,为后续的分析实战弄素材~ 2019年7月18日修正: 因为高德和百度对于GPS的加 ...
- python二手房使用教程_python实战:基于链家网二手房数据解析任务
实战:基于链家网二手房数据解析任务 页面:https://bd.ke.com/ershoufang/ 获取如下信息: 标题 位置 房屋信息 关注人数 / 发布时间 房屋总价 房屋单价 备注信息 1.导 ...
- python爬取南京市房价_基于python的链家小区房价爬取——仅需60行代码
简介 首先打开相关网页(北京链家小区信息). 注意本博客的代码适用于爬取某个城市的小区二手房房价信息. 如果需要爬取其他信息,可修改代码,链家的数据获取的基本逻辑都差不多. 效果展示 因为只需要60行 ...
最新文章
- Jconsole远程监控tomcat 的JVM内存(linux、windows)
- Sublime Text3 自定义快速生成代码
- python如何读取excel数据-python怎么从excel中读取数据?
- 王哲:Cocos2d-x 3.0引擎带来了什么?
- docker OOM问题排查思路
- 分布式与人工智能课程(part14)--深度学习
- 凭自己本事单的身是一种怎样的体验?你根本配不上如此优秀的我!
- 50-20-190-配置-Flink 配置文件详解-flink-conf.yaml
- img 居中_解决img标签下方出现的小空隙
- 管理站点复制 【Windows Server 2019】活动目录(Acitve Directory)——在同一区域安装多台域控制器
- c语言进阶.pdf,C语言程序设计进阶:文件.pdf
- opencv python 实现灰度图像和彩色图像直方图全局均衡化和自适应均衡化
- Android学习——写个小实例
- 再谈javascript图片预加载技术(转)
- 遗传算法python实现求最大值_python 遗传算法求函数极值的实现代码
- java web 常见面试题_2019最新Javaweb面试题及答案
- Visual Studio MFC编程 程序调试时所遇到的问题及解决方法
- 新手兼职也能月入5000的副业项目,几乎零门槛
- C语言实训心得体会(精选4篇)
- 我的世界热力膨胀JAVA_我的世界TE4教程热力膨胀能源炉的合成与使用数据
热门文章
- Win7连接手机提示ADB Interface 找不到驱动程序解决办法
- 坦桑尼亚签证办理攻略
- UED的设计流程:原型,模型,demo
- 【转】Android 加速度传感器 (G-Sensor)
- 计算机软件大全学习,[计算机软件及应用]CAD入门学习大全.ppt
- U盘插3.0口不识别,插2.0口识别
- jcg 836 固件_JCG Studios – ArkDroid正式发布
- Linux -bash java command not found 解决方法
- Kubernetes 上免费的容器存储及容灾备份恢复方案
- 背书快背崩溃了!还是背了忘,怎么办!