python爬虫学习-scrapy爬取链家房源信息并存储(翻页)
2019-05-13 18:48
399 查看
爬取链家租房频道的房源信息,含翻页,含房间详情页的内容爬取。
items.py
[code]import scrapy class ScrapytestItem(scrapy.Item): # define the fields for your item here like: title = scrapy.Field()#房源名称 price = scrapy.Field()#价格 url = scrapy.Field()#详情页地址 introduce_item = scrapy.Field()#房源描述
pipelines.py
[code]import json class ScrapytestPipeline(object): #打开文件 def open_spider(self,spider): self.file = open('58_chuzu.txt','w',encoding='utf-8') print('文件被打开了') #写入文件 def process_item(self, item, spider): line = '{}\n'.format(json.dumps(dict(item),ensure_ascii=False)) self.file.write(line) return item #关闭文件 def close_spider(self,spider): self.file.close() print('文件被关闭了')
spider
[code]import scrapy from ..items import ScrapytestItem from scrapy.http import Request class SpiderCity58Spider(scrapy.Spider): name = 'spider_city_58'#必不可少的爬虫名字 allowed_domains = ['lianjia.com'] start_urls = ['https://bj.lianjia.com/zufang/'] def parse(self, response): #提取页面上的信息 info_list = response.xpath('//*[@id="content"]/div[1]/div[1]/div') for i in info_list: item = ScrapytestItem() item['title'] = i.xpath('normalize-space(./div/p[1]/a/text())').extract() item['price'] = i.xpath('./div/span/em/text()').extract() url = i.xpath('./div/p[1]/a/@href').extract_first()#相对地址补全为绝对地址 item['url'] = response.urljoin(url) #获取详情页的URL if item['url']:#判断URL是否为空 yield Request( item['url'], callback = self.detail_parse, meta = {'item':item},#只接受字典类型的赋值,将item传递给detali_parse() priority = 10, dont_filter = True ) #获取翻页URL for page in range(2,5): url = 'https://bj.lianjia.com/zufang/pg{}/'.format(str(page))#提取翻页链接 test_request = Request(url,callback = self.parse) yield test_request #获取详情页的信息 def detail_parse(self,response): item = response.meta['item'] item['introduce_item'] = response.xpath('//*[@id="desc"]/ul/li/p[1]/text()').extract() return item
本来打算爬58的,但是58的反爬策略我还无法破解,所以换成了链家。
目前爬了4页,证明可行。后续添加时间控制等代码即可
相关文章推荐
- Python中使用Scrapy爬虫抓取上海链家房价信息
- python 爬虫学习三(Scrapy 实战,豆瓣爬取电影信息)
- [Python Scrapy爬虫] 二.翻页爬取农产品信息并保存本地
- Python3爬虫学习之MySQL数据库存储爬取的信息详解
- Python的Scrapy爬虫框架简单学习笔记
- Python爬虫实战一之使用Beautiful Soup抓取百度招聘信息并存储excel文件
- Python爬虫从入门到放弃(十八)之 Scrapy爬取所有知乎用户信息(上)
- Python爬虫框架Scrapy 学习笔记 10.1 -------【实战】 抓取天猫某网店所有宝贝详情
- Python爬虫框架Scrapy实战之定向批量获取职位招聘信息
- Python爬虫Scrapy框架学习第1课 Win10系统下scrapy安装和环境搭建
- Python自动化开发学习-分布式爬虫(scrapy-redis)
- Python的学习笔记DAY7---关于爬虫(2)之Scrapy初探
- python爬虫框架scrapy学习笔记
- python基础_Scrapy爬虫基础学习一
- python,scrapy爬虫sql之爬取数据存储到mysql的piplelines.py配置
- Python爬虫项目--爬取自如网房源信息
- python抓取链家房源信息
- 爬虫技术 -- 进阶学习(十)网易新闻页面信息抓取(htmlagilitypack搭配scrapysharp)
- Python爬虫框架Scrapy 学习笔记 10.2 -------【实战】 抓取天猫某网店所有宝贝详情
- Python3爬虫学习之将爬取的信息保存到本地的方法详解