当前位置: 首页 > news >正文

济南建设网站电商网站开发

济南建设网站,电商网站开发,做pc端网站行情,做网站普洱本帖最后由 zhukeming123 于 2019-11-17 01:13 编辑链家房源爬虫,基于scrapy框架,数据存储在MongoDB由于链家的限制房源列表最多显示1000页,3000条数据,所以这里通过小区信息爬取所有房源。# -*- coding: utf-8 -*-import scrapyf…

本帖最后由 zhukeming123 于 2019-11-17 01:13 编辑

链家房源爬虫,基于scrapy框架,数据存储在MongoDB

由于链家的限制房源列表最多显示1000页,3000条数据,所以这里通过小区信息爬取所有房源。

# -*- coding: utf-8 -*-

import scrapy

from scrapy import Selector

import json

import re

from house_spider.items import LianjiaVillageItem, LianjiaHouseItem

class LianjiaSpider(scrapy.Spider):

name = 'lianjia'

allowed_domains = ['cq.lianjia.com']

start_urls = ['cq.lianjia.com']

def __init__(self, **kwargs):

super().__init__(**kwargs)

self.base_url = 'https://cq.lianjia.com'

def start_requests(self):

request_url = 'https://cq.lianjia.com/xiaoqu/'

yield scrapy.Request(url=request_url, callback=self.parse_district_links)

def parse_district_links(self, response):

"""提取地区链接"""

sel = Selector(response)

links = sel.css("div[data-role='ershoufang'] div:first-child a::attr(href)").extract()

for link in links:

url = self.base_url + link

yield scrapy.Request(url=url, callback=self.parse_bizcircle_links)

def parse_bizcircle_links(self, response):

"""提取商圈链接"""

sel = Selector(response)

links = sel.css("div[data-role='ershoufang'] div:nth-child(2) a::attr(href)").extract()

for link in links:

url = self.base_url + link

yield scrapy.Request(url=url, callback=self.parse_village_list, meta={"ref": url})

def parse_village_list(self, response):

"""提取小区链接"""

sel = Selector(response)

links = sel.css(".listContent .xiaoquListItem .img::attr(href)").extract()

for link in links:

yield scrapy.Request(url=link, callback=self.parse_village_detail)

# page

page_data = sel.css(".house-lst-page-box::attr(page-data)").extract_first()

page_data = json.loads(page_data)

if page_data['curPage'] < page_data['totalPage']:

url = response.meta["ref"] + 'pg' + str(page_data['curPage'] + 1)

yield scrapy.Request(url=url, callback=self.parse_village_list, meta=response.meta)

def parse_village_detail(self, response):

"""提取小区详情"""

village_url = response.url

sel = Selector(response)

zone = sel.css('.xiaoquDetailbreadCrumbs .l-txt a::text').extract()

latitude = 0

longitude = 0

try:

html = response.body.decode().replace('\r', '')

local = html[html.find('resblockPosition:'):html.find('resblockName') - 1]

m = re.search('(\d.*\d),(\d.*\d)', local)

longitude = m.group(1)

latitude = m.group(2)

except Exception:

pass

item = LianjiaVillageItem()

item['id'] = village_url.replace(self.base_url + '/xiaoqu/', '').replace('/', '')

item['name'] = sel.css('.detailHeader .detailTitle::text').extract_first()

item['address'] = sel.css('.detailHeader .detailDesc::text').extract_first()

item['latitude'] = latitude

item['longitude'] = longitude

item['zone'] = ','.join(zone)

item['year'] = sel.css('.xiaoquInfo .xiaoquInfoItem:nth-child(1) .xiaoquInfoContent::text').extract_first()

item['build_type'] = sel.css('.xiaoquInfo .xiaoquInfoItem:nth-child(2) .xiaoquInfoContent::text').extract_first()

item['property_costs'] = sel.css('.xiaoquInfo .xiaoquInfoItem:nth-child(3) .xiaoquInfoContent::text').extract_first()

item['property_company'] = sel.css('.xiaoquInfo .xiaoquInfoItem:nth-child(4) .xiaoquInfoContent::text').extract_first()

item['developers'] = sel.css('.xiaoquInfo .xiaoquInfoItem:nth-child(5) .xiaoquInfoContent::text').extract_first()

item['buildings'] = sel.css('.xiaoquInfo .xiaoquInfoItem:nth-child(6) .xiaoquInfoContent::text').extract_first()

item['total_house'] = sel.css('.xiaoquInfo .xiaoquInfoItem:nth-child(7) .xiaoquInfoContent::text').extract_first()

print(item['name'])

yield item

# 小区房源 https://cq.lianjia.com/ershoufang/c3620038190566370/

url = self.base_url + "/ershoufang/c" + item['id'] + "/"

yield scrapy.Request(url=url, callback=self.parse_house_list, meta={"ref": url})

def parse_house_list(self, response):

"""提取房源链接"""

sel = Selector(response)

# 链家有时小区查询不到数据

total = sel.css('.resultDes .total span::text').extract_first()

total = int(total)

if total > 0:

# 提取房源链接

links = sel.css(".sellListContent li .info .title a::attr(href)").extract()

for link in links:

yield scrapy.Request(url=link, callback=self.parse_house_detail)

# 链接分页

page_data = sel.css(".house-lst-page-box::attr(page-data)").extract_first()

page_data = json.loads(page_data)

if page_data['curPage'] == 1 and page_data['totalPage'] > 1:

price = response.url.replace(self.base_url + '/ershoufang/', '')

for x in range(2, page_data['totalPage'] + 1, 1):

url = self.base_url + '/ershoufang/' + 'pg' + str(x) + price

yield scrapy.Request(url=url, callback=self.parse_house_list)

def parse_house_detail(self, response):

"""提取房源信息"""

sel = Selector(response)

item = LianjiaHouseItem()

item['房屋Id'] = response.url.replace(self.base_url + '/ershoufang/', '').replace('.html', '')

item['标题'] = sel.css('.title-wrapper .title .main::text').extract_first()

item['售价'] = sel.css('.overview .content .price .total::text').extract_first()

item['小区'] = sel.css('.overview .content .aroundInfo .communityName a.info::text').extract_first()

item['小区ID'] = sel.css('.overview .content .aroundInfo .communityName a.info::attr(href)').extract_first().replace('/xiaoqu/', '').replace('/', '')

item['房屋户型'] = sel.css('#introduction .base .content ul li:nth-child(1)::text').extract_first()

item['所在楼层'] = sel.css('#introduction .base .content ul li:nth-child(2)::text').extract_first()

item['建筑面积'] = sel.css('#introduction .base .content ul li:nth-child(3)::text').extract_first()

item['户型结构'] = sel.css('#introduction .base .content ul li:nth-child(4)::text').extract_first()

item['套内面积'] = sel.css('#introduction .base .content ul li:nth-child(5)::text').extract_first()

item['建筑类型'] = sel.css('#introduction .base .content ul li:nth-child(6)::text').extract_first()

item['房屋朝向'] = sel.css('#introduction .base .content ul li:nth-child(7)::text').extract_first()

item['建筑结构'] = sel.css('#introduction .base .content ul li:nth-child(8)::text').extract_first()

item['装修情况'] = sel.css('#introduction .base .content ul li:nth-child(9)::text').extract_first()

item['梯户比例'] = sel.css('#introduction .base .content ul li:nth-child(10)::text').extract_first()

item['配备电梯'] = sel.css('#introduction .base .content ul li:nth-child(11)::text').extract_first()

item['产权年限'] = sel.css('#introduction .base .content ul li:nth-child(12)::text').extract_first()

item['挂牌时间'] = sel.css('#introduction .transaction .content ul li:nth-child(1) span:nth-child(2)::text').extract_first()

item['交易权属'] = sel.css('#introduction .transaction .content ul li:nth-child(2) span:nth-child(2)::text').extract_first()

item['上次交易'] = sel.css('#introduction .transaction .content ul li:nth-child(3) span:nth-child(2)::text').extract_first()

item['房屋用途'] = sel.css('#introduction .transaction .content ul li:nth-child(4) span:nth-child(2)::text').extract_first()

item['房屋年限'] = sel.css('#introduction .transaction .content ul li:nth-child(5) span:nth-child(2)::text').extract_first()

item['产权所属'] = sel.css('#introduction .transaction .content ul li:nth-child(6) span:nth-child(2)::text').extract_first()

item['抵押信息'] = sel.css('#introduction .transaction .content ul li:nth-child(7) span:nth-child(2)::attr(title)').extract_first()

item['房本备件'] = sel.css('#introduction .transaction .content ul li:nth-child(8) span:nth-child(2)::text').extract_first()

yield item

TIM截图20191117002122.png (13.3 KB, 下载次数: 8)

2019-11-17 00:22 上传

TIM截图20191117002133.png (170.03 KB, 下载次数: 4)

2019-11-17 00:22 上传

TIM截图20191117002145.png (181.91 KB, 下载次数: 4)

2019-11-17 00:22 上传

完整项目 git地址:https://github.com/oldriverz/house_spider

http://www.lbrq.cn/news/2464579.html

相关文章:

  • 亚洲做性视频网站seo专员工作容易学吗
  • 浙江网络公司网站建设营业推广策略
  • 百万综合文字论文网址的内容优化是什么意思?
  • 渭南做网站的公司网站的优化策略方案
  • 广州建网站维护公司seo网站优化流程
  • 济阳做网站网络营销过程步骤
  • 武昌网站建设哪家便宜seo托管
  • 大型网站技术架构演进与性能优化刷移动关键词优化
  • 做网站竞价怎么找客户推广软件的app
  • 吉工之家找工作建筑工作seo公司是做什么的
  • wordpress4.8seo关键词
  • 广州网站公司推荐uc浏览器关键词排名优化
  • 北京南站到北京站怎么走推广点击器
  • 南城网站优化公司seo研究学院
  • 手机网站首页设计link友情买卖
  • 百度一直不收录网站互联网推广平台有哪些公司
  • 中国免费域名申请网站百度推广怎么做最好
  • 郑州网站权重互联网搜索引擎有哪些
  • 武汉网站推广技巧安卓神级系统优化工具
  • 企业网站管理系统项目文档站长统计免费下载
  • 帮公司制作一个网站是如何收费广州seo全网营销
  • 公司简介在哪里查询软件优化
  • 邢台网站建设免费做网站排名电商最好卖的十大产品
  • 免费网站推广渠道百度指数名词解释
  • 网站开发能赚多少钱收录批量查询
  • 手机网站 触屏关键词seo排名怎么做的
  • jsp 做网站还是php网络营销软文范例500
  • 网站的开发和建设有什么区别外贸网站优化公司
  • 企业网站优化公司公司员工培训内容有哪些
  • 高端网站建设服务最新新闻播报
  • Zama+OpenZeppelin:将机密智能合约带入 DeFi 和数字资产领域
  • Oracle 11g RAC数据库实例重启的两种方式
  • OCR工具集下载与保姆级安装教程!!
  • 2D游戏背景滚动教程(JavaSwing)
  • vscode 字体的跟换
  • 安卓上的迷之K_1171477665