当前位置: 首页 > news >正文

备案域名网站大全wordpress 开发教程

备案域名网站大全,wordpress 开发教程,公司网站建设费用会计处理,重庆平台网站建设价格Scrapy框架详解及其基本使用 scrapy框架原理 Scrapy是一个为了爬取网站数据#xff0c;提取结构性数据而编写的应用框架。 其可以应用在数据挖掘#xff0c;信息处理或存储历史数据等一系列的程序中。其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的#xff0c; 也可…Scrapy框架详解及其基本使用 scrapy框架原理 Scrapy是一个为了爬取网站数据提取结构性数据而编写的应用框架。 其可以应用在数据挖掘信息处理或存储历史数据等一系列的程序中。其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的 也可以应用在获取API所返回的数据(例如 Amazon Associates Web Services ) 或者通用的网络爬虫。Scrapy用途广泛可以用于数据挖掘、监测和自动化测试。 Scrapy 使用了 Twisted异步网络库来处理网络通讯。整体架构大致如下   Scrapy主要包括了以下组件 引擎(Scrapy)用来处理整个系统的数据流处理, 触发事务(框架核心)调度器(Scheduler)用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. 可以想像成一个URL抓取网页的网址或者说是链接的优先队列, 由它来决定下一个要抓取的网址是什么, 同时去除重复的网址下载器(Downloader)用于下载网页内容, 并将网页内容返回给蜘蛛(Scrapy下载器是建立在twisted这个高效的异步模型上的)爬虫(Spiders)爬虫是主要干活的, 用于从特定的网页中提取自己需要的信息, 即所谓的实体(Item)。用户也可以从中提取出链接,让Scrapy继续抓取下一个页面项目管道(Pipeline)负责处理爬虫从网页中抽取的实体主要的功能是持久化实体、验证实体的有效性、清除不需要的信息。当页面被爬虫解析后将被发送到项目管道并经过几个特定的次序处理数据。下载器中间件(Downloader Middlewares)位于Scrapy引擎和下载器之间的框架主要是处理Scrapy引擎与下载器之间的请求及响应。爬虫中间件(Spider Middlewares)介于Scrapy引擎和爬虫之间的框架主要工作是处理蜘蛛的响应输入和请求输出。调度中间件(Scheduler Middewares)介于Scrapy引擎和调度之间的中间件从Scrapy引擎发送到调度的请求和响应。Scrapy运行流程大概如下 1、从spider中获取到初始url给引擎告诉引擎帮我给调度器 2、引擎将初始url给调度器调度器安排入队列 3、调度器告诉引擎已经安排好并把url给引擎告诉引擎给下载器进行下载 4、引擎将url给下载器下载器下载页面源码 5、下载器告诉引擎已经下载好了并把页面源码response给到引擎 6、引擎拿着response给到spiderspider解析数据、提取数据 7、spider将提取到的数据给到引擎告诉引擎帮我把新的url给到调度器入队列把信息给到Item Pipelines进行保存 8、Item Pipelines将提取到的数据保存保存好后告诉引擎可以进行下一个url的提取了 9、循环3-8步直到调度器中没有url关闭网站若url下载失败了会返回重新下载。 基本使用 创建项目的基本过程 Scrapy# 创建项目在当前目录中创建中创建一个项目文件类似于Djangoscrapy startproject sp1生成目录如下sp1- sp1- spiders         目录放置创建的爬虫应用- middlewares.py 中间件- items.py 格式化与pipelines.py一同做持久化- pipelines.py 持久化- settings.py 配置文件- scrapy.cfg 配置# 创建爬虫应用cd sp1scrapy genspider xiaohuar xiaohuar.com # 创建了xiaohuar.pyscrapy genspider baidu baidu.com # 创建了baidu.py# 展示爬虫应用列表scrapy list# 执行爬虫进入projectscrapy crawl baiduscrapy crawl baidu --nolog 文件说明 注意一般创建爬虫文件时以网站域名命名 项目实战 实战目标对目标站点所有语录作者标签进行爬取并存如MongoDB中 目标站点分析Quates to scrape 流程框架 爬虫实战 明确目标---items.py明确想要抓取的目标定义需要爬取的信息字段 # -*- coding: utf-8 -*-import scrapyclass QuoteItem(scrapy.Item):# define the fields for your item here like:# name scrapy.Field()text scrapy.Field() #语录内容author scrapy.Field() #作者tags scrapy.Field() #标签 制作爬虫---quotes.py解析数据并提取信息和新的url # -*- coding: utf-8 -*- import scrapyfrom quotetutorial.items import QuoteItemclass QuotesSpider(scrapy.Spider):name quotesallowed_domains [quotes.toscrape.com]start_urls [http://quotes.toscrape.com/]def parse(self, response):#print(response.text)quotes response.css(.quote) #获取每行的全部信息for quote in quotes:item QuoteItem() #创建获取对象text quote.css(.text::text).extract_first() #*::text 用于获取文本信息axtract_first() 用于获得第一个文本信息*author quote.css(.author::text).extract_first()tags quote.css(.tags .tag::text).extract() #没有指定获取第一个---获取所有满足条件的item[text] textitem[author] authoritem[tags] tagsyield itemnext response.css(.pager .next a::attr(href)).extract_first() #获取元素属性信息url response.urljoin(next) #把连接拼接起来yield scrapy.Request(urlurl,callbackself.parse) #回调函数 存储内容---pipelines.py设计管道存储内容。当spider收集好Item后会将Item由字典组成的列表传递到Item Pipeline这些Item Pipeline组件按定义的顺序处理Item # -*- coding: utf-8 -*- import pymongofrom scrapy.exceptions import DropItemclass TextPipeline(object): #对语录进行处理当长度超过50时截断然后在后面加*...*def __init__(self):self.limit 50def process_item(self, item, spider):if item[text]:if len(item[text]) self.limit:item[text] item[text][0:self.limit].rstrip() ...return itemelse:return DropItem(Miss Text)class MongoPipeline(object): #链接数据库def __init__(self ,mongo_uri, mongo_db):self.mongo_uri mongo_uriself.mongo_db mongo_dbclassmethoddef from_crawler(cls, crawler): #从ettings中拿到需要的配置信息类方法return cls(mongo_uricrawler.settings.get(MONGO_URI),mongo_dbcrawler.settings.get(MONGO_DB))def open_spider(self,spider): #初始化数据库self.client pymongo.MongoClient(self.mongo_uri)self.db self.client[self.mongo_db]def process_item(self, item ,spider): #向数据库插入数据name item.__class__.__name__self.db[name].insert(dict(item))return itemdef close_spider(self ,spider):self.client.close() 相关配置---settings.py为了启动Item Pipelines组件必须将类添加到settings.py的ITEM_PIPELINES中此处只有一个pipeline类因此找到ITEM_PIPELINES,打开代码 # -*- coding: utf-8 -*-# Scrapy settings for quotetutorial project # # For simplicity, this file contains only settings considered important or # commonly used. You can find more settings consulting the documentation: # # https://doc.scrapy.org/en/latest/topics/settings.html # https://doc.scrapy.org/en/latest/topics/downloader-middleware.html # https://doc.scrapy.org/en/latest/topics/spider-middleware.htmlBOT_NAME quotetutorialSPIDER_MODULES [quotetutorial.spiders] NEWSPIDER_MODULE quotetutorial.spidersMONGO_URI localhost MONGO_DB quotestutorial# Crawl responsibly by identifying yourself (and your website) on the user-agent #USER_AGENT quotetutorial (http://www.yourdomain.com)# Obey robots.txt rules ROBOTSTXT_OBEY True# Configure maximum concurrent requests performed by Scrapy (default: 16) #CONCURRENT_REQUESTS 32# Configure a delay for requests for the same website (default: 0) # See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay # See also autothrottle settings and docs #DOWNLOAD_DELAY 3 # The download delay setting will honor only one of: #CONCURRENT_REQUESTS_PER_DOMAIN 16 #CONCURRENT_REQUESTS_PER_IP 16# Disable cookies (enabled by default) #COOKIES_ENABLED False# Disable Telnet Console (enabled by default) #TELNETCONSOLE_ENABLED False# Override the default request headers: #DEFAULT_REQUEST_HEADERS { # Accept: text/html,application/xhtmlxml,application/xml;q0.9,*/*;q0.8, # Accept-Language: en, #}# Enable or disable spider middlewares # See https://doc.scrapy.org/en/latest/topics/spider-middleware.html #SPIDER_MIDDLEWARES { # quotetutorial.middlewares.QuotetutorialSpiderMiddleware: 543, #}# Enable or disable downloader middlewares # See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html #DOWNLOADER_MIDDLEWARES { # quotetutorial.middlewares.QuotetutorialDownloaderMiddleware: 543, #}# Enable or disable extensions # See https://doc.scrapy.org/en/latest/topics/extensions.html #EXTENSIONS { # scrapy.extensions.telnet.TelnetConsole: None, #}# Configure item pipelines # See https://doc.scrapy.org/en/latest/topics/item-pipeline.html ITEM_PIPELINES {quotetutorial.pipelines.TextPipeline: 300,quotetutorial.pipelines.MongoPipeline: 400, }# Enable and configure the AutoThrottle extension (disabled by default) # See https://doc.scrapy.org/en/latest/topics/autothrottle.html #AUTOTHROTTLE_ENABLED True # The initial download delay #AUTOTHROTTLE_START_DELAY 5 # The maximum download delay to be set in case of high latencies #AUTOTHROTTLE_MAX_DELAY 60 # The average number of requests Scrapy should be sending in parallel to # each remote server #AUTOTHROTTLE_TARGET_CONCURRENCY 1.0 # Enable showing throttling stats for every response received: #AUTOTHROTTLE_DEBUG False# Enable and configure HTTP caching (disabled by default) # See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings #HTTPCACHE_ENABLED True #HTTPCACHE_EXPIRATION_SECS 0 #HTTPCACHE_DIR httpcache #HTTPCACHE_IGNORE_HTTP_CODES [] #HTTPCACHE_STORAGE scrapy.extensions.httpcache.FilesystemCacheStorage 注意如果有多个item pipelines的话多种保存方式需要在ITEM_PIPELINES中配置类后面的“300”随意设置。      分配给每个类的整型值确定了它们的运行顺序。数值越低组件的优先级越高运行顺序越靠前。 启动项目 scrapy crawl quotes 把获得的内容保存 scrapy crawl quotes -o quotes.{json | jl | csv | xml | pickle | marshal}    转载于:https://www.cnblogs.com/darwinli/p/9485505.html
http://www.pierceye.com/news/397911/

相关文章:

  • 黑客攻击的网站网站开发需要多少钱app
  • 海南建设厅评审网站织梦网站加网站地图
  • 清迈城市建设网站外贸搜索网站
  • 怎样做网站公司网站建设维护管理
  • 伊犁网站制作珠海做网站的公司
  • 乐山市规划和建设局网站房地产基础知识
  • 网站给假冒伪劣产品做推广鞍山网站制作一般需要多少钱
  • 番禺网站开发哪里好ppt模板免费下载完整版免费简约
  • 哪个公司做企业网站好济南网站优化推广公司电话
  • 深圳网站建设外包公司网站流量提供商
  • 网站建设优化服务信息wordpress下载类插件
  • 深圳做网站google推广百度优化是什么
  • 网站开发技术服务费分享经济网站怎么建设
  • 免费seo网站推广在线观看360免费wifi创建失败
  • 服装网站开发嵌入式硬件开发
  • 上海建设厅网站那些网站可以做自媒体
  • 如何查看一个网站流量网店美工课程心得体会
  • 邯郸的网站建设无锡做网站品牌公司
  • 汇编做网站门户网站建设 知乎
  • 教育云平台网站建设云南小程序定制开发
  • 企业自助建站策划方案横沥网站设计
  • 网站开发搜索功能怎么实现中小网站建设都有哪些方案
  • 学科网站建设网页制作和网页制作
  • 公司网站模板大全网站文章编辑
  • 旅游网站建设的总结wordpress多域名移动主题
  • 深圳做网站推荐哪家公司好附近广告公司联系电话
  • 网站建设和网站优化哪个更重要提供邯郸网站建设
  • 做网站一般把宽度做多少合肥优化
  • 石家庄做网站公司汉狮价格猴痘的治疗方法
  • 自己有网站 做app吗深圳罗湖企业网站推广