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

广州市地图最新版 高清晰优化seo是什么意思

广州市地图最新版 高清晰,优化seo是什么意思,怎么网上接网站开发单自己做,湛江设计公司Scrapy入门 1、Scrapy概述2、PyCharm搭建Scrapy环境3、Scrapy使用四部曲4、Scrapy入门案例4.1、明确目标4.2、制作爬虫4.3、存储数据4.4、运行爬虫 1、Scrapy概述 Scrapy是一个由Python语言开发的适用爬取网站数据、提取结构性数据的Web应用程序框架。主要用于数据挖掘、信息处…

Scrapy入门

    • 1、Scrapy概述
    • 2、PyCharm搭建Scrapy环境
    • 3、Scrapy使用四部曲
    • 4、Scrapy入门案例
      • 4.1、明确目标
      • 4.2、制作爬虫
      • 4.3、存储数据
      • 4.4、运行爬虫

1、Scrapy概述

Scrapy是一个由Python语言开发的适用爬取网站数据、提取结构性数据的Web应用程序框架。主要用于数据挖掘、信息处理、数据存储和自动化测试等。通过Scrapy框架实现一个爬虫,只需要少量的代码,就能够快速的网络抓取

Scrapy框架5大组件(架构):

在这里插入图片描述

  • Scrapy引擎(Scrapy Engine):Scrapy引擎是整个框架的核心,负责Spider、ItemPipeline、Downloader、Scheduler间的通讯、数据传递等
  • 调度器(Scheduler):网页URL的优先队列,主要负责处理引擎发送的请求,并按一定方式排列调度,当引擎需要时,交还给引擎
  • 下载器(Downloader):负责下载引擎发送的所有Requests请求资源,并将其获取到的Responses交还给引擎,由引擎交给Spider来处理
  • 爬虫(Spider):用户定制的爬虫,用于从特定网页中提取信息(实体Item),负责处理所有Responses,从中提取数据,并将需要跟进的URL提交给引擎,再次进入调度器
  • 实体管道(Item Pipeline):用于处理Spider中获取的实体,并进行后期处理(详细分析、过滤、持久化存储等)

其他组件:

  • 下载中间件(Downloader Middlewares):一个可以自定义扩展下载功能的组件
  • Spider中间件(Spider Middlewares):一个可以自定扩展和操作引擎和Spider间通信的组件

官方文档:https://docs.scrapy.org

入门文档:https://doc.scrapy.org/en/latest/intro/tutorial.html

2、PyCharm搭建Scrapy环境

1)新建一个爬虫项目ScrapyDemo

2)在Terminal终端安装所需模块

Scrapy基于Twisted,Twisted是一个异步网络框架,主要用于提高爬虫的下载速度

pip install scrapy
pip install twisted

如果报错:

ERROR: Failed building wheel for twisted
error: Microsoft Visual C++ 14.0 or greater is required

则需要下载对应的whl文件安装:

Python扩展包whl文件下载:https://www.lfd.uci.edu/~gohlke/pythonlibs/#

ctrl+f查找需要的whl文件,点击下载对应版本

安装:

pip install whl文件绝对路径

例如:

pip install F:\PyWhl\Twisted-20.3.0-cp38-cp38m-win_amd64.whl

3)在Terminal终端创建爬虫项目ScrapyDemo

scrapy startproject ScrapyDemo

生成项目目录结构

4)在spiders文件夹下创建核心爬虫文件SpiderDemo.py

最终项目结构及说明:

ScrapyDemo/                              爬虫项目├── ScrapyDemo/                      爬虫项目目录    │      ├── spiders/                  爬虫文件│      │      ├── __init__.py   │      │      └── SpiderDemo.py      自定义核心功能文件│      ├── __init__.py   │      ├── items.py                  爬虫目标数据│      ├── middlewares.py            中间件、代理  │      ├── pipelines.py              管道,用于处理爬取的数据    │      └── settings.py               爬虫配置文件└── scrapy.cfg                       项目配置文件

3、Scrapy使用四部曲

1)明确目标

明确爬虫的目标网站

明确需要爬取实体(属性):items.py

定义:属性名 = scrapy.Field()

2)制作爬虫

自定义爬虫核心功能文件:spiders/SpiderDemo.py

3)存储数据

设计管道存储爬取内容:settings.py、pipelines.py

4)运行爬虫

方式1:在Terminal终端执行(cmd执行需要切到项目根目录下)

scrapy crawl dangdang(爬虫名)

cmd切换操作:

切盘:F:
切换目录:cd A/B/...

方式2:在PyCharm执行文件

在爬虫项目目录下创建运行文件run.py,右键运行

4、Scrapy入门案例

4.1、明确目标

1)爬取当当网手机信息:https://category.dangdang.com/cid4004279.html

2)明确需要爬取实体属性:items.py

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.htmlimport scrapy# 1)明确目标
# 1.2)明确需要爬取实体属性
class ScrapyDemoItem(scrapy.Item):# define the fields for your item here like:# name = scrapy.Field()# 名称name = scrapy.Field()# 价格price = scrapy.Field()

4.2、制作爬虫

SpiderDemo.py

# 入门案例# 1)明确目标
# 1.1)爬取当当网手机信息:https://category.dangdang.com/cid4004279.html# 2)制作爬虫
import scrapy
from scrapy.http import Response
from ..items import ScrapyDemoItemclass SpiderDemo(scrapy.Spider):# 爬虫名称,运行爬虫时使用的值name = "dangdang"# 爬虫域,允许访问的域名allowed_domains = ['category.dangdang.com']# 爬虫地址:起始URL:第一次访问是域名start_urls = ['https://category.dangdang.com/cid4004279.html']# 翻页分析# 第1页:https://category.dangdang.com/cid4004279.html# 第2页:https://category.dangdang.com/pg2-cid4004279.html# 第3页:https://category.dangdang.com/pg3-cid4004279.html# ......page = 1# 请求响应处理def parse(self, response: Response):li_list = response.xpath('//ul[@id="component_47"]/li')for li in li_list:# 商品名称name = li.xpath('.//img/@alt').extract_first()print(name)# 商品价格price = li.xpath('.//p[@class="price"]/span[1]/text()').extract_first()print(price)# 获取一个实体对象就交给管道pipelinesdemo = ScrapyDemoItem(name=name, price=price)# 封装item数据后,调用yield将控制权给管道,管道拿到item后返回该程序yield demo# 每一页爬取逻辑相同,只需要将执行下一页的请求再次调用parse()方法即可if self.page <= 10:self.page += 1url = rf"https://category.dangdang.com/pg{str(self.page)}-cid4004279.html"# scrapy.Request为scrapy的请求# yield中断yield scrapy.Request(url=url, callback=self.parse)

补充:Response对象的属性和方法

'''
1)获取响应的字符串
response.text
2)获取响应的二进制数据
response.body
3)解析响应内容
response.xpath()
'''

4.3、存储数据

settings.py

# Scrapy settings for ScrapyDemo project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://docs.scrapy.org/en/latest/topics/settings.html
#     https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://docs.scrapy.org/en/latest/topics/spider-middleware.html# 3)存储数据
# 3.1)爬虫配置、打开通道和添加通道# 爬虫项目名
BOT_NAME = "ScrapyDemo"SPIDER_MODULES = ["ScrapyDemo.spiders"]
NEWSPIDER_MODULE = "ScrapyDemo.spiders"# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = "ScrapyDemo (+http://www.yourdomain.com)"
# User-Agent配置
USER_AGENT = 'Mozilla/5.0'# Obey robots.txt rules
# 是否遵循机器人协议(默认True),为了避免一些爬取限制需要改为False
ROBOTSTXT_OBEY = False# 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://docs.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
# 下载延迟(单位:s),用于控制爬取的频率
#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(默认False)
#COOKIES_ENABLED = False# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False# Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
#    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
#    "Accept-Language": "en",
#}
# 请求头
DEFAULT_REQUEST_HEADERS = {"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Language": "en",
}# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#    "ScrapyDemo.middlewares.ScrapydemoSpiderMiddleware": 543,
#}# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
#    "ScrapyDemo.middlewares.ScrapydemoDownloaderMiddleware": 543,
#}# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#    "scrapy.extensions.telnet.TelnetConsole": None,
#}# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
#ITEM_PIPELINES = {
#    "ScrapyDemo.pipelines.ScrapydemoPipeline": 300,
#}# 项目管道
ITEM_PIPELINES = {# 管道可以有多个,后面的数字是优先级(范围:1-1000),值越小优先级越高# 爬取网页'scrapy_dangdang.pipelines.ScrapyDemoPipeline': 300,# 保存数据'scrapy_dangdang.pipelines.ScrapyDemoSinkPiepline': 301,
}# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.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://docs.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"# Set settings whose default value is deprecated to a future-proof value
REQUEST_FINGERPRINTER_IMPLEMENTATION = "2.7"
TWISTED_REACTOR = "twisted.internet.asyncioreactor.AsyncioSelectorReactor"
FEED_EXPORT_ENCODING = "utf-8"# 设置日志输出等级(默认DEBUG)与日志存放的路径
LOG_LEVEL = 'INFO'
# LOG_FILE = "spider.log"

pipelines.py

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html# useful for handling different item types with a single interface
from itemadapter import ItemAdapter# 3)存储数据
# 3.2)使用管道存储数据
# 若使用管道,则必须在settings.py中开启管道import os
import csv# 爬取网页
class ScrapyDemoPipeline:# 数据item交给管道输出def process_item(self, item, spider):print(item)return item# 保存数据
class ScrapyDemoSinkPiepline:# item为yield后面的ScrapyDemoItem对象,字典类型def process_item(self, item, spider):with open(r'C:\Users\cc\Desktop\scrapy_test.csv', 'a', newline='', encoding='utf-8') as csvfile:# 定义表头fields = ['name', 'price']writer = csv.DictWriter(csvfile, fieldnames=fields)writer.writeheader()# 写入数据writer.writerow(item)

4.4、运行爬虫

run.py

# 4)运行爬虫from scrapy import cmdlinecmdline.execute('scrapy crawl dangdang'.split())

其他文件不动,本案例运行会报错:

ERROR: Twisted-20.3.0-cp38-cp38m-win_amd64.whl is not a supported wheel on this platform
builtins.ModuleNotFoundError: No module named 'scrapy_dangdang'

原因大概是Twisted版本兼容问题,暂未解决,后续补充


文章转载自:
http://dinncomisusage.wbqt.cn
http://dinncofearlessly.wbqt.cn
http://dinncocomputus.wbqt.cn
http://dinncolevis.wbqt.cn
http://dinncopowerbook.wbqt.cn
http://dinncodiscordant.wbqt.cn
http://dinncorenovator.wbqt.cn
http://dinncohydrozincite.wbqt.cn
http://dinncojudea.wbqt.cn
http://dinncozootheism.wbqt.cn
http://dinnconavaid.wbqt.cn
http://dinncoregarding.wbqt.cn
http://dinncoprepublication.wbqt.cn
http://dinncoshakespearean.wbqt.cn
http://dinncosalesperson.wbqt.cn
http://dinncolatona.wbqt.cn
http://dinncosteepy.wbqt.cn
http://dinncomountainward.wbqt.cn
http://dinncoglomera.wbqt.cn
http://dinncoanalgesia.wbqt.cn
http://dinncocontroversial.wbqt.cn
http://dinncosteerage.wbqt.cn
http://dinncohy.wbqt.cn
http://dinncoflavone.wbqt.cn
http://dinncodeexcite.wbqt.cn
http://dinncogovernorship.wbqt.cn
http://dinncohornfels.wbqt.cn
http://dinncodedicatory.wbqt.cn
http://dinncojalousie.wbqt.cn
http://dinncowpc.wbqt.cn
http://dinncolanner.wbqt.cn
http://dinncopastromi.wbqt.cn
http://dinncomotherland.wbqt.cn
http://dinncopipkin.wbqt.cn
http://dinnconecropsy.wbqt.cn
http://dinncoculpability.wbqt.cn
http://dinncotophamper.wbqt.cn
http://dinncohyperlipemia.wbqt.cn
http://dinncomoneylender.wbqt.cn
http://dinncoirvingite.wbqt.cn
http://dinncobrownie.wbqt.cn
http://dinncoborosilicate.wbqt.cn
http://dinncospearfisherman.wbqt.cn
http://dinncologo.wbqt.cn
http://dinncosciagram.wbqt.cn
http://dinncocorset.wbqt.cn
http://dinncoclod.wbqt.cn
http://dinncocaesarist.wbqt.cn
http://dinncoscoline.wbqt.cn
http://dinncotroat.wbqt.cn
http://dinncochelifer.wbqt.cn
http://dinncotortrix.wbqt.cn
http://dinncoesther.wbqt.cn
http://dinncokench.wbqt.cn
http://dinncocragsman.wbqt.cn
http://dinnconapier.wbqt.cn
http://dinncovolga.wbqt.cn
http://dinncooncidium.wbqt.cn
http://dinncoawkwardly.wbqt.cn
http://dinncopereon.wbqt.cn
http://dinncosenorita.wbqt.cn
http://dinncovancouver.wbqt.cn
http://dinncomisnomer.wbqt.cn
http://dinncocholon.wbqt.cn
http://dinncodissave.wbqt.cn
http://dinncograniteware.wbqt.cn
http://dinncoflied.wbqt.cn
http://dinncoradiovisor.wbqt.cn
http://dinncohideously.wbqt.cn
http://dinncovijayavada.wbqt.cn
http://dinncopalaeobotany.wbqt.cn
http://dinncoaedicule.wbqt.cn
http://dinncosporicide.wbqt.cn
http://dinncovortices.wbqt.cn
http://dinncopopliteal.wbqt.cn
http://dinncounpersuasive.wbqt.cn
http://dinncoprotractile.wbqt.cn
http://dinncofab.wbqt.cn
http://dinncoparka.wbqt.cn
http://dinncotractate.wbqt.cn
http://dinncotinware.wbqt.cn
http://dinncoscrummage.wbqt.cn
http://dinncopotty.wbqt.cn
http://dinncotheomorphic.wbqt.cn
http://dinncoharangue.wbqt.cn
http://dinncoscourer.wbqt.cn
http://dinncohyalography.wbqt.cn
http://dinncosnooperscope.wbqt.cn
http://dinncoinceptisol.wbqt.cn
http://dinncoflako.wbqt.cn
http://dinncoevisceration.wbqt.cn
http://dinncotriradius.wbqt.cn
http://dinncojdbc.wbqt.cn
http://dinncolegalise.wbqt.cn
http://dinncoantipsychiatry.wbqt.cn
http://dinncotariffless.wbqt.cn
http://dinncoreflectoscope.wbqt.cn
http://dinncotellable.wbqt.cn
http://dinncoprf.wbqt.cn
http://dinncoaloft.wbqt.cn
http://www.dinnco.com/news/122583.html

相关文章:

  • 淘宝做首页热点的什么网站百度网盘人工客服
  • 网站开发与系统开发百度经验官网登录
  • 深圳科技公司排名10网站推广优化外链
  • 项目外包流程seo的外链平台有哪些
  • wordpress滑块验证码杭州百度首页优化
  • 自建网站成都网站seo思路
  • 建设电子商务网站期末考试网站建设企业咨询
  • 做搬家网站推广在那好国际最新十大新闻事件
  • 网站传送门怎么做站长收录平台
  • 做网站最省钱淘宝运营
  • 武汉做医疗器械公司网站的合肥百度seo代理
  • 开个捕鱼网站怎么做免费网页模板网站
  • 做网站卖东西流程关键词排名优化如何
  • 广西庆海建设发展有限公司网站网站排名优化推广
  • 扫二维码直接进网站怎么做怎么样才能引流客人进店
  • 汕头网站建设怎么收费目前小说网站排名
  • 临沂做网站建设的公司哪家好广州营销型网站
  • 找公司做网站需要咨询什么问题电商网站对比
  • 青岛网站制作公司网络郑州seo外包阿亮
  • 介绍商务网站开发流程上海网站优化公司
  • 网站与数据库的联系谷歌推广新手教程
  • 福建设计招标网站百度手机助手下载安装最新版
  • 公司建站网站全世界足球排名国家
  • 口碑营销5t理论网站搜索引擎优化的方法
  • 全国二级建造师查询官网seo自动优化软件安卓
  • 如何借用别人网站做模板windows清理优化大师
  • 福州网站建设的公司哪家好神起网络游戏推广平台
  • 网页设计制作单位seo关键词优化公司哪家好
  • 做兼职的网站有哪些国际重大新闻
  • wordpress 用户名长度搜索优化整站优化