IT桔子分布式项目2

2023-12-26 07:08
文章标签 项目 分布式 桔子

本文主要是介绍IT桔子分布式项目2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

项目实现:

items.py

# items.py# -*- coding: utf-8 -*-
import scrapyclass CompanyItem(scrapy.Item):# 公司id (url数字部分)info_id = scrapy.Field()# 公司名称company_name = scrapy.Field()# 公司口号slogan = scrapy.Field()# 分类scope = scrapy.Field()# 子分类sub_scope = scrapy.Field()# 所在城市city = scrapy.Field()# 所在区域area = scrapy.Field()# 公司主页home_page = scrapy.Field()# 公司标签tags = scrapy.Field()# 公司简介company_intro = scrapy.Field()# 公司全称:company_full_name = scrapy.Field()# 成立时间:found_time = scrapy.Field()# 公司规模:company_size = scrapy.Field()# 运营状态company_status = scrapy.Field()# 投资情况列表:包含获投时间、融资阶段、融资金额、投资公司tz_info = scrapy.Field()# 团队信息列表:包含成员姓名、成员职称、成员介绍tm_info = scrapy.Field()# 产品信息列表:包含产品名称、产品类型、产品介绍pdt_info = scrapy.Field()

settings.py


# -*- coding: utf-8 -*-BOT_NAME = 'itjuzi'SPIDER_MODULES = ['itjuzi.spiders']
NEWSPIDER_MODULE = 'itjuzi.spiders'# Enables scheduling storing requests queue in redis.
SCHEDULER = "scrapy_redis.scheduler.Scheduler"# Ensure all spiders share same duplicates filter through redis.
DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"# REDIS_START_URLS_AS_SET = TrueCOOKIES_ENABLED = FalseDOWNLOAD_DELAY = 1.5# 支持随机下载延迟
RANDOMIZE_DOWNLOAD_DELAY = True# Obey robots.txt rules
ROBOTSTXT_OBEY = FalseITEM_PIPELINES = {'scrapy_redis.pipelines.RedisPipeline': 300
}DOWNLOADER_MIDDLEWARES = {# 该中间件将会收集失败的页面,并在爬虫完成后重新调度。(失败情况可能由于临时的问题,例如连接超时或者HTTP 500错误导致失败的页面)'scrapy.downloadermiddlewares.retry.RetryMiddleware': 80,# 该中间件提供了对request设置HTTP代理的支持。您可以通过在 Request 对象中设置 proxy 元数据来开启代理。'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware': 100,'itjuzi.middlewares.RotateUserAgentMiddleware': 200,
}REDIS_HOST = "192.168.199.108"
REDIS_PORT = 6379

middlewares.py

# -*- coding: utf-8 -*-from scrapy.contrib.downloadermiddleware.useragent import UserAgentMiddleware
import random# User-Agetn 下载中间件
class RotateUserAgentMiddleware(UserAgentMiddleware):def __init__(self, user_agent=''):self.user_agent = user_agentdef process_request(self, request, spider):# 这句话用于随机选择user-agentua = random.choice(self.user_agent_list)request.headers.setdefault('User-Agent', ua)user_agent_list = ["Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1","Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6","Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6","Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5","Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3","Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3","Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3","Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3","Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24","Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10","Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/533.17.8 (KHTML, like Gecko) Version/5.0.1 Safari/533.17.8","Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.2 Safari/533.18.5","Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.1.17) Gecko/20110123 (like Firefox/3.x) SeaMonkey/2.0.12","Mozilla/5.0 (Windows NT 5.2; rv:10.0.1) Gecko/20100101 Firefox/10.0.1 SeaMonkey/2.7.1","Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/532.8 (KHTML, like Gecko) Chrome/4.0.302.2 Safari/532.8","Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.464.0 Safari/534.3","Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.15 Safari/534.13","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.54 Safari/535.2","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.36 Safari/535.7","Mozilla/5.0 (Macintosh; U; Mac OS X Mach-O; en-US; rv:2.0a) Gecko/20040614 Firefox/3.0.0 ","Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.0.3) Gecko/2008092414 Firefox/3.0.3","Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5","Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.14) Gecko/20110218 AlexaToolbar/alxf-2.0 Firefox/3.6.14","Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"]

spiders/juzi.py


# -*- coding: utf-8 -*-from bs4 import BeautifulSoup
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rulefrom scrapy_redis.spiders import RedisCrawlSpider
from itjuzi.items import CompanyItemclass ITjuziSpider(RedisCrawlSpider):name = 'itjuzi'allowed_domains = ['www.itjuzi.com']# start_urls = ['http://www.itjuzi.com/company']redis_key = 'itjuzispider:start_urls'rules = [# 获取每一页的链接Rule(link_extractor=LinkExtractor(allow=('/company\?page=\d+'))),# 获取每一个公司的详情Rule(link_extractor=LinkExtractor(allow=('/company/\d+')), callback='parse_item')]def parse_item(self, response):soup = BeautifulSoup(response.body, 'lxml')# 开头部分: //div[@class="infoheadrow-v2 ugc-block-item"]cpy1 = soup.find('div', class_='infoheadrow-v2')if cpy1:# 公司名称://span[@class="title"]/b/text()[1]company_name = cpy1.find(class_='title').b.contents[0].strip().replace('\t', '').replace('\n', '')# 口号: //div[@class="info-line"]/pslogan = cpy1.find(class_='info-line').p.get_text()# 分类:子分类//span[@class="scope c-gray-aset"]/a[1]scope_a = cpy1.find(class_='scope c-gray-aset').find_all('a')# 分类://span[@class="scope c-gray-aset"]/a[1]scope = scope_a[0].get_text().strip() if len(scope_a) > 0 else ''# 子分类:# //span[@class="scope c-gray-aset"]/a[2]sub_scope = scope_a[1].get_text().strip() if len(scope_a) > 1 else ''# 城市+区域://span[@class="loca c-gray-aset"]/acity_a = cpy1.find(class_='loca c-gray-aset').find_all('a')# 城市://span[@class="loca c-gray-aset"]/a[1]city = city_a[0].get_text().strip() if len(city_a) > 0 else ''# 区域://span[@class="loca c-gray-aset"]/a[2]area = city_a[1].get_text().strip() if len(city_a) > 1 else ''# 主页://a[@class="weblink marl10"]/@hrefhome_page = cpy1.find(class_='weblink marl10')['href']# 标签://div[@class="tagset dbi c-gray-aset"]/atags = cpy1.find(class_='tagset dbi c-gray-aset').get_text().strip().strip().replace('\n', ',')#基本信息://div[@class="block-inc-info on-edit-hide"]cpy2 = soup.find('div', class_='block-inc-info on-edit-hide')if cpy2:# 公司简介://div[@class="block-inc-info on-edit-hide"]//div[@class="des"]company_intro = cpy2.find(class_='des').get_text().strip()# 公司全称:成立时间:公司规模:运行状态://div[@class="des-more"]cpy2_content = cpy2.find(class_='des-more').contents# 公司全称://div[@class="des-more"]/div[1]company_full_name = cpy2_content[1].get_text().strip()[len('公司全称:'):] if cpy2_content[1] else ''# 成立时间://div[@class="des-more"]/div[2]/span[1]found_time = cpy2_content[3].contents[1].get_text().strip()[len('成立时间:'):] if cpy2_content[3] else ''# 公司规模://div[@class="des-more"]/div[2]/span[2]company_size = cpy2_content[3].contents[3].get_text().strip()[len('公司规模:'):] if cpy2_content[3] else ''#运营状态://div[@class="des-more"]/div[3]company_status = cpy2_content[5].get_text().strip() if cpy2_content[5] else ''# 主体信息:main = soup.find('div', class_='main')# 投资情况://table[@class="list-round-v2 need2login"]# 投资情况,包含获投时间、融资阶段、融资金额、投资公司tz = main.find('table', 'list-round-v2')tz_list = []if tz:all_tr = tz.find_all('tr')for tr in all_tr:tz_dict = {}all_td = tr.find_all('td')tz_dict['tz_time'] = all_td[0].span.get_text().strip()tz_dict['tz_round'] = all_td[1].get_text().strip()tz_dict['tz_finades'] = all_td[2].get_text().strip()tz_dict['tz_capital'] = all_td[3].get_text().strip().replace('\n', ',')tz_list.append(tz_dict)# 团队信息:成员姓名、成员职称、成员介绍tm = main.find('ul', class_='list-prodcase limited-itemnum')tm_list = []if tm:for li in tm.find_all('li'):tm_dict = {}tm_dict['tm_m_name'] = li.find('span', class_='c').get_text().strip()tm_dict['tm_m_title'] = li.find('span', class_='c-gray').get_text().strip()tm_dict['tm_m_intro'] = li.find('p', class_='mart10 person-des').get_text().strip()tm_list.append(tm_dict)# 产品信息:产品名称、产品类型、产品介绍pdt = main.find('ul', class_='list-prod limited-itemnum')pdt_list = []if pdt:for li in pdt.find_all('li'):pdt_dict = {}pdt_dict['pdt_name'] = li.find('h4').b.get_text().strip()pdt_dict['pdt_type'] = li.find('span', class_='tag yellow').get_text().strip()pdt_dict['pdt_intro'] = li.find(class_='on-edit-hide').p.get_text().strip()pdt_list.append(pdt_dict)item = CompanyItem()item['info_id'] = response.url.split('/')[-1:][0]item['company_name'] = company_nameitem['slogan'] = sloganitem['scope'] = scopeitem['sub_scope'] = sub_scopeitem['city'] = cityitem['area'] = areaitem['home_page'] = home_pageitem['tags'] = tagsitem['company_intro'] = company_introitem['company_full_name'] = company_full_nameitem['found_time'] = found_timeitem['company_size'] = company_sizeitem['company_status'] = company_statusitem['tz_info'] = tz_listitem['tm_info'] = tm_listitem['pdt_info'] = pdt_listreturn item

scrapy.cfg


# Automatically created by: scrapy startproject
#
# For more information about the [deploy] section see:
# https://scrapyd.readthedocs.org/en/latest/deploy.html[settings]
default = itjuzi.settings[deploy]
#url = http://localhost:6800/
project = itjuzi

运行:

Slave端:
scrapy runspider juzi.pyMaster端:
redis-cli > lpush itjuzispider:start_urls http://www.itjuzi.com/company

演示效果:


这篇关于IT桔子分布式项目2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/538507

相关文章

java如何分布式锁实现和选型

《java如何分布式锁实现和选型》文章介绍了分布式锁的重要性以及在分布式系统中常见的问题和需求,它详细阐述了如何使用分布式锁来确保数据的一致性和系统的高可用性,文章还提供了基于数据库、Redis和Zo... 目录引言:分布式锁的重要性与分布式系统中的常见问题和需求分布式锁的重要性分布式系统中常见的问题和需求

Golang使用etcd构建分布式锁的示例分享

《Golang使用etcd构建分布式锁的示例分享》在本教程中,我们将学习如何使用Go和etcd构建分布式锁系统,分布式锁系统对于管理对分布式系统中共享资源的并发访问至关重要,它有助于维护一致性,防止竞... 目录引言环境准备新建Go项目实现加锁和解锁功能测试分布式锁重构实现失败重试总结引言我们将使用Go作

javafx 如何将项目打包为 Windows 的可执行文件exe

《javafx如何将项目打包为Windows的可执行文件exe》文章介绍了三种将JavaFX项目打包为.exe文件的方法:方法1使用jpackage(适用于JDK14及以上版本),方法2使用La... 目录方法 1:使用 jpackage(适用于 JDK 14 及更高版本)方法 2:使用 Launch4j(

Redis分布式锁使用及说明

《Redis分布式锁使用及说明》本文总结了Redis和Zookeeper在高可用性和高一致性场景下的应用,并详细介绍了Redis的分布式锁实现方式,包括使用Lua脚本和续期机制,最后,提到了RedLo... 目录Redis分布式锁加锁方式怎么会解错锁?举个小案例吧解锁方式续期总结Redis分布式锁如果追求

Docker集成CI/CD的项目实践

《Docker集成CI/CD的项目实践》本文主要介绍了Docker集成CI/CD的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录一、引言1.1 什么是 CI/CD?1.2 docker 在 CI/CD 中的作用二、Docke

SpringBoot项目引入token设置方式

《SpringBoot项目引入token设置方式》本文详细介绍了JWT(JSONWebToken)的基本概念、结构、应用场景以及工作原理,通过动手实践,展示了如何在SpringBoot项目中实现JWT... 目录一. 先了解熟悉JWT(jsON Web Token)1. JSON Web Token是什么鬼

手把手教你idea中创建一个javaweb(webapp)项目详细图文教程

《手把手教你idea中创建一个javaweb(webapp)项目详细图文教程》:本文主要介绍如何使用IntelliJIDEA创建一个Maven项目,并配置Tomcat服务器进行运行,过程包括创建... 1.启动idea2.创建项目模板点击项目-新建项目-选择maven,显示如下页面输入项目名称,选择

Jenkins中自动化部署Spring Boot项目的全过程

《Jenkins中自动化部署SpringBoot项目的全过程》:本文主要介绍如何使用Jenkins从Git仓库拉取SpringBoot项目并进行自动化部署,通过配置Jenkins任务,实现项目的... 目录准备工作启动 Jenkins配置 Jenkins创建及配置任务源码管理构建触发器构建构建后操作构建任务

Nginx、Tomcat等项目部署问题以及解决流程

《Nginx、Tomcat等项目部署问题以及解决流程》本文总结了项目部署中常见的four类问题及其解决方法:Nginx未按预期显示结果、端口未开启、日志分析的重要性以及开发环境与生产环境运行结果不一致... 目录前言1. Nginx部署后未按预期显示结果1.1 查看Nginx的启动情况1.2 解决启动失败的

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template