使用scrapy框架爬取链家网站租房信息
2018-11-30 10:31
295 查看
不清楚的可以去GitHub点击进入https://github.com/Agile929/lianjia.git
这是主代码,其他代码可以去GitHub查看
# -*- coding: utf-8 -*- import scrapy from lianjia.settings import USER_AGENT_LIST import random import re from lianjia.items import LianjiaItem class LianjiaZufangSpider(scrapy.Spider): name = 'lianjia_zufang' allowed_domains = ['lianjia.com'] base_url = "https://sz.lianjia.com/zufang/pg" page = 0 start_urls = [base_url + str(page)] def parse(self, response): referer = self.base_url + str(self.page) headers = {"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN,zh;q=0.9", "Connection": "keep-alive", "Host": "sz.lianjia.com", "Referer": referer, "Upgrade-Insecure-Requests": "1", "User-Agent": random.choice(USER_AGENT_LIST)} node_list = response.xpath('//div[@class="wrapper"]//ul[@id="house-lst"]/li') if not node_list: return for node in node_list: detail_url = node.xpath('./div[@class="info-panel"]/h2/a/@href').extract_first() yield scrapy.Request(url=detail_url, headers=headers, callback=self.parse_detail, meta={"detail_url": detail_url}) self.page += 1 yield scrapy.Request(url=self.base_url + str(self.page), headers=headers, callback=self.parse) def parse_detail(self, response): detail_html = response.body.decode("utf-8") node_list = response.xpath('//div[@class="content-wrapper"]/div[@class="overview"]/div[@class="content zf-content"]') for node in node_list: item = LianjiaItem() item["address"] = node.xpath('./div[@class="zf-room"]/p[7]/a[1]/text()').extract_first() + "-" + node.xpath('./div[@class="zf-room"]/p[7]/a[2]/text()').extract_first() item['price'] = re.compile(r'<span class="total">(.*?)</span>', re.S).findall(detail_html)[0] item["address_detail"] = node.xpath('./div[@class="zf-room"]/p[6]/a/text()').extract_first() item["detail_url"] = response.meta["detail_url"] item["area"] = node.xpath('./div[@class="zf-room"]/p[1]/text()').extract_first() item["floor"] = node.xpath('./div[@class="zf-room"]/p[3]/text()').extract_first() item["house_type"] = node.xpath('./div[@class="zf-room"]/p[2]/text()').extract_first() item["release_time"] = node.xpath('./div[@class="zf-room"]/p[8]/text()').extract_first() item["content"] = response.xpath( '//div[@class="content-wrapper"]//div[@class="title"]/h1/text()').extract_first() yield item
执行结果
有问题请留言
相关文章推荐
- 【python爬虫02】使用Scrapy框架爬取拉勾网招聘信息
- Scrapy框架的学习(7. 了解Scrapy中的debug信息以及Scrapy shell的使用)
- scrapy框架下爬取51job网站信息,并存储到表格中
- 实战--Scrapy框架爬去网站信息
- Python使用scrapy抓取网站sitemap信息的方法
- windows下使用python的scrapy爬虫框架,爬取个人博客文章内容信息
- Python中使用Scrapy爬虫抓取上海链家房价信息
- 网络爬虫:使用Scrapy框架编写一个抓取书籍信息的爬虫服务
- 基于scrapy框架的关于58同城招聘网站信息的爬取
- 使用scrapy 模拟登陆网站后 抓取会员中心相关信息
- P_010.~慢慢悠悠~使用Python的Scrapy框架成功爬取豆瓣电影的全部信息
- 爬虫框架之Scrapy——爬取某招聘信息网站
- Scrapy框架——CrawlSpider爬取某招聘信息网站
- 网络爬虫---用scrapy框架爬取当当网商品信息实战将信息写入数据库(主要是对scrapy框架的熟悉和初步使用)
- 使用scrapy框架简单快速爬取淘宝商品信息
- 使用Scrapy框架爬取艺龙网机票信息
- javaEE01-使用很HTML的排版标签编写“网站信息页面”,使用图片标签编写“图面显示页面”,使用列表标签编写“友情链接页面”,使用表格标签编写“首页”,框架标签表现“后台页面”
- ios项目开发(天气预报项目):使用正则获取 weather.com.cn网站信息
- 使用ThinkPHP框架快速开发网站(多图)
- 使用scrapy框架爬取自己的博文(3)