python爬虫项目(scrapy-redis分布式爬取房天下租房信息)
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分布式爬取房天下租房信息)的更多相关文章
- 爬虫--scrapy+redis分布式爬取58同城北京全站租房数据
作业需求: 1.基于Spider或者CrawlSpider进行租房信息的爬取 2.本机搭建分布式环境对租房信息进行爬取 3.搭建多台机器的分布式环境,多台机器同时进行租房数据爬取 建议:用Pychar ...
- Python爬取房天下二手房信息
一.相关知识 BeautifulSoup4使用 python将信息写入csv import csv with open("11.csv","w") as csv ...
- Python爬虫学习三------requests+BeautifulSoup爬取简单网页
第一次第一次用MarkDown来写博客,先试试效果吧! 昨天2018俄罗斯世界杯拉开了大幕,作为一个伪球迷,当然也得为世界杯做出一点贡献啦. 于是今天就编写了一个爬虫程序将腾讯新闻下世界杯专题的相关新 ...
- scrapy-redis + Bloom Filter分布式爬取tencent社招信息
scrapy-redis + Bloom Filter分布式爬取tencent社招信息 什么是scrapy-redis 什么是 Bloom Filter 为什么需要使用scrapy-redis + B ...
- scrapy-redis分布式爬取tencent社招信息
scrapy-redis分布式爬取tencent社招信息 什么是scrapy-redis 目标任务 安装爬虫 创建爬虫 编写 items.py 编写 spiders/tencent.py 编写 pip ...
- Python爬虫学习(6): 爬取MM图片
为了有趣我们今天就主要去爬取以下MM的图片,并将其按名保存在本地.要爬取的网站为: 大秀台模特网 1. 分析网站 进入官网后我们发现有很多分类: 而我们要爬取的模特中的女模内容,点进入之后其网址为:h ...
- python爬虫实践(二)——爬取张艺谋导演的电影《影》的豆瓣影评并进行简单分析
学了爬虫之后,都只是爬取一些简单的小页面,觉得没意思,所以我现在准备爬取一下豆瓣上张艺谋导演的“影”的短评,存入数据库,并进行简单的分析和数据可视化,因为用到的只是比较多,所以写一篇博客当做笔记. 第 ...
- python网络爬虫之scrapy 调试以及爬取网页
Shell调试: 进入项目所在目录,scrapy shell “网址” 如下例中的: scrapy shell http://www.w3school.com.cn/xml/xml_syntax.as ...
- Python爬虫基础--分布式爬取贝壳网房屋信息(Client)
1. client_code01 2. client_code02 3. 这个时候运行多个client就可以分布式进行数据爬取.
随机推荐
- Winform将一个窗体显示在另一个窗体中
private void ShowForm(Form Indexform) { Form1 form1 = new Form1(); form1 .TopLevel = false; form1 .P ...
- CUDA执行模型
1.设备管理和查看: cudaError_t cudaGetDeviceProperties(cudaDeviceProp * prop,int device) 用户可以通过这个函数来查看自己GPU设 ...
- Lesson 25 Do the English speak English?
Text I arrived London at last. The railway station was big, black and dark. I did not know the way t ...
- [Swift]LeetCode70. 爬楼梯 | Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [Swift]LeetCode213. 打家劫舍 II | House Robber II
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [Swift]LeetCode983. 最低票价 | Minimum Cost For Tickets
In a country popular for train travel, you have planned some train travelling one year in advance. ...
- Json数组转换字符串、字符串转换原数组......
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Python内置函数(65)——type
英文文档: class type(object) class type(name, bases, dict) With one argument, return the type of an obje ...
- 版本管理工具Git(二)GitLab部署和配置
安装 # 安装依赖包 sudo yum install -y curl policycoreutils-python openssh-server # 启用并启动SSHD sudo systemctl ...
- 使用 curl 进行 ssl 认证
目录 SSL 认证 认证实现 问题解决 curl不支持 https SSL certificate problem, verify that the CA cert is OK curl: (60) ...