python爬虫scrapy项目(二)

  爬取目标:房天下全国租房信息网站(起始url:http://zu.fang.com/cities.aspx)

  爬取内容:城市;名字;出租方式;价格;户型;面积;地址;交通

  反反爬措施:设置随机user-agent、设置请求延时操作、

1、开始创建项目

1 scrapy startproject fang

2、进入fang文件夹,执行启动spider爬虫文件代码,编写爬虫文件。

1 scrapy genspider zufang "zu.fang.com"

  命令执行完,用Python最好的IDE---pycharm打开该文件目录

3、编写该目录下的items.py文件,设置你需要爬取的字段。

 import scrapy

 class HomeproItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field() city = scrapy.Field() #城市
title = scrapy.Field() # 名字
rentway = scrapy.Field() # 出租方式
price = scrapy.Field() #价格
housetype = scrapy.Field() # 户型
area = scrapy.Field() # 面积
address = scrapy.Field() # 地址
traffic = scrapy.Field() # 交通

4、进入spiders文件夹,打开hr.py文件,开始编写爬虫文件

 # -*- coding: utf-8 -*-
import scrapy
from homepro.items import HomeproItem
from scrapy_redis.spiders import RedisCrawlSpider
# scrapy.Spider
class HomeSpider(RedisCrawlSpider):
name = 'home'
allowed_domains = ['zu.fang.com']
# start_urls = ['http://zu.fang.com/cities.aspx'] redis_key = 'homespider:start_urls'
def parse(self, response):
hrefs = response.xpath('//div[@class="onCont"]/ul/li/a/@href').extract()
for href in hrefs:
href = 'http:'+ href
yield scrapy.Request(url=href,callback=self.parse_city,dont_filter=True) def parse_city(self, response):
page_num = response.xpath('//div[@id="rentid_D10_01"]/span[@class="txt"]/text()').extract()[0].strip('共页')
# print('*' * 100)
# print(page_num)
# print(response.url) for page in range(1, int(page_num)):
if page == 1:
url = response.url
else:
url = response.url + 'house/i%d' % (page + 30)
print('*' * 100)
print(url)
yield scrapy.Request(url=url, callback=self.parse_houseinfo, dont_filter=True) def parse_houseinfo(self, response):
divs = response.xpath('//dd[@class="info rel"]')
for info in divs:
city = info.xpath('//div[@class="guide rel"]/a[2]/text()').extract()[0].rstrip("租房")
title = info.xpath('.//p[@class="title"]/a/text()').extract()[0]
rentway = info.xpath('.//p[@class="font15 mt12 bold"]/text()')[0].extract().replace(" ", '').lstrip('\r\n')
housetype = info.xpath('.//p[@class="font15 mt12 bold"]/text()')[1].extract().replace(" ", '')
area = info.xpath('.//p[@class="font15 mt12 bold"]/text()')[2].extract().replace(" ", '')
addresses = info.xpath('.//p[@class ="gray6 mt12"]//span/text()').extract()
address = '-'.join(i for i in addresses)
try:
des = info.xpath('.//p[@class ="mt12"]//span/text()').extract()
traffic = '-'.join(i for i in des)
except Exception as e:
traffic = "暂无详细信息" p_name = info.xpath('.//div[@class ="moreInfo"]/p/text()').extract()[0]
p_price = info.xpath('.//div[@class ="moreInfo"]/p/span/text()').extract()[0]
price = p_price + p_name item = HomeproItem()
item['city'] = city
item['title'] = title
item['rentway'] = rentway
item['price'] = price
item['housetype'] = housetype
item['area'] = area
item['address'] = address
item['traffic'] = traffic
yield item

5、设置setting.py文件,配置scrapy运行的相关内容

 # 指定使用scrapy-redis的调度器
SCHEDULER = "scrapy_redis.scheduler.Scheduler" # 指定使用scrapy-redis的去重
DUPEFILTER_CLASS = 'scrapy_redis.dupefilter.RFPDupeFilter' # 指定排序爬取地址时使用的队列,
# 默认的 按优先级排序(Scrapy默认),由sorted set实现的一种非FIFO、LIFO方式。
SCHEDULER_QUEUE_CLASS = 'scrapy_redis.queue.SpiderPriorityQueue' REDIS_HOST = '10.8.153.73'
REDIS_PORT = 6379
# 是否在关闭时候保留原来的调度器和去重记录,True=保留,False=清空
SCHEDULER_PERSIST = True

6、然后把代码发给其他附属机器,分别启动.子程序redis链接主服务器redis。

 redis-cli   -h  主服务器ip

7、主服务器先启动redis-server,再启动redis-cli

 lpush homespider:start_urls   起始的url 

python爬虫项目(scrapy-redis分布式爬取房天下租房信息)的更多相关文章

  1. 爬虫--scrapy+redis分布式爬取58同城北京全站租房数据

    作业需求: 1.基于Spider或者CrawlSpider进行租房信息的爬取 2.本机搭建分布式环境对租房信息进行爬取 3.搭建多台机器的分布式环境,多台机器同时进行租房数据爬取 建议:用Pychar ...

  2. Python爬取房天下二手房信息

    一.相关知识 BeautifulSoup4使用 python将信息写入csv import csv with open("11.csv","w") as csv ...

  3. Python爬虫学习三------requests+BeautifulSoup爬取简单网页

    第一次第一次用MarkDown来写博客,先试试效果吧! 昨天2018俄罗斯世界杯拉开了大幕,作为一个伪球迷,当然也得为世界杯做出一点贡献啦. 于是今天就编写了一个爬虫程序将腾讯新闻下世界杯专题的相关新 ...

  4. scrapy-redis + Bloom Filter分布式爬取tencent社招信息

    scrapy-redis + Bloom Filter分布式爬取tencent社招信息 什么是scrapy-redis 什么是 Bloom Filter 为什么需要使用scrapy-redis + B ...

  5. scrapy-redis分布式爬取tencent社招信息

    scrapy-redis分布式爬取tencent社招信息 什么是scrapy-redis 目标任务 安装爬虫 创建爬虫 编写 items.py 编写 spiders/tencent.py 编写 pip ...

  6. Python爬虫学习(6): 爬取MM图片

    为了有趣我们今天就主要去爬取以下MM的图片,并将其按名保存在本地.要爬取的网站为: 大秀台模特网 1. 分析网站 进入官网后我们发现有很多分类: 而我们要爬取的模特中的女模内容,点进入之后其网址为:h ...

  7. python爬虫实践(二)——爬取张艺谋导演的电影《影》的豆瓣影评并进行简单分析

    学了爬虫之后,都只是爬取一些简单的小页面,觉得没意思,所以我现在准备爬取一下豆瓣上张艺谋导演的“影”的短评,存入数据库,并进行简单的分析和数据可视化,因为用到的只是比较多,所以写一篇博客当做笔记. 第 ...

  8. python网络爬虫之scrapy 调试以及爬取网页

    Shell调试: 进入项目所在目录,scrapy shell “网址” 如下例中的: scrapy shell http://www.w3school.com.cn/xml/xml_syntax.as ...

  9. Python爬虫基础--分布式爬取贝壳网房屋信息(Client)

    1. client_code01 2. client_code02 3. 这个时候运行多个client就可以分布式进行数据爬取.

随机推荐

  1. ueditor编辑器多图上传为什么顺序打乱了

    我上一个版本用的是ueditor1.3.6,自从1.4.2版以后,“前端上传模块统一改用webuploader”,ueditor在多图上传一直考虑漏掉了图片顺序的问题. 我的网站在用户上传图片文章的时 ...

  2. 3-3 Hadoop集群完全分布式配置部署

    Hadoop集群完全分布式配置部署 下面的部署步骤,除非说明是在哪个服务器上操作,否则默认为在所有服务器上都要操作.为了方便,使用root用户. 1.准备工作 1.1 centOS6服务器3台 手动指 ...

  3. 树莓派3B+ HDMI连接显示屏 因供电问题而不能进入系统

    1.config.txt文件中hdmi_force_hotplug=1前面的注释符号"#"一定要去掉. 2.完成上述操作后,树莓派通过HDMI连接屏幕,一直在开机画面循环重复,却不 ...

  4. [Swift]LeetCode241. 为运算表达式设计优先级 | Different Ways to Add Parentheses

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  5. [Swift]LeetCode671. 二叉树中第二小的节点 | Second Minimum Node In a Binary Tree

    Given a non-empty special binary tree consisting of nodes with the non-negative value, where each no ...

  6. [Swift]LeetCode929. 独特的电子邮件地址 | Unique Email Addresses

    Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...

  7. [Swift]LeetCode966.元音拼写检查器 | Vowel Spellchecker

    Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word ...

  8. python获取当前运行程序的名字

    import os filename = os.path.abspath(__file__) print filename 打印结果: E:\bluedon\test.py

  9. Eureka介绍

    1.  Eureka是什么 Eureka是一个基于REST的服务,主要用于AWS云中的定位服务,以实现中间层服务器的负载平衡和故障转移 在 Spring Cloud 微服务架构中通常用作注册中心 我们 ...

  10. 带着萌新看springboot源码13(手写一个自己的starter)

    springboot的最强大的就是那些xxxAutoconfiguration,但是这些xxxAutoConfiguration又依赖那些starter,只有导入了这些场景启动器(starter),我 ...