import scrapy
from firstBlood.items import FirstbloodItem

class FirstSpider(scrapy.Spider):
爬虫文件的名称:爬虫源文件的唯一标识
name = 'first'
允许的域名:
allowed_domains = ['www.baidu.com']
起始的url列表:列表中的列表元素会被scrapy自动的进行请求发送
start_urls = ['https://dig.chouti.com/']
解析数据
基于终端指令的持久化存储
def parse(self, response):
data_list = []
div_list = response.xpath('/html/body/main/div/div/div[1]/div/div[2]/div[1]')
for div in div_list:
#注意:xpath返回的列表中的列表元素是Selector对象,我们要解析获取的字符串的数据是存储在该对象中
#必须经过一个extract()的操作才可以将改对象中存储的字符串的数据获取
# content = div.xpath('./div/div/div[1]/a/text()')[0].extract()
content = div.xpath('./div/div/div[1]/a/text()').extract_first()
#xpath返回的列表中的列表元素有多个(Selector对象),想要将每一个列表元素对应的Selector中的字符串取出改如何操作?response.xpath('/div//text()').extract()
print(content) #<Selector xxx='dsdsd' data="短发hi的说法绝对是">
data_list.append(content)
return data_list

#管道的持久化存储
def parse(self, response):
div_list = response.xpath('/html/body/main/div/div/div[1]/div/div[2]/div[1]')
for div in div_list:
#注意:xpath返回的列表中的列表元素是Selector对象,我们要解析获取的字符串的数据是存储在该对象中
#必须经过一个extract()的操作才可以将改对象中存储的字符串的数据获取
# content = div.xpath('./div/div/div[1]/a/text()')[0].extract()
content = div.xpath('./div/div/div[1]/a/text()').extract_first()
item = FirstbloodItem()
item['content'] = content
#xpath返回的列表中的列表元素有多个(Selector对象),想要将每一个列表元素对应的Selector中的字符串取出改如何操作?response.xpath('/div//text()').extract()
print(content) #<Selector xxx='dsdsd' data="短发hi的说法绝对是">

yield item #将item对象提交给管道

items.py:

import scrapy


class FirstbloodItem(scrapy.Item):
# define the fields for your item here like:
content = scrapy.Field()#Field是一个万能的数据类型

pipelines.py:

#专门用作于持久化存储
class FirstbloodPipeline(object):
fp = None
def open_spider(self,spider):
"""重写父类方法"""
print('我只会在爬虫开始的时候执行一次!!!')
self.fp = open('./data.txt','w',encoding='utf-8')
def process_item(self, item, spider):
"""处理item"""
content = item['content']
self.fp.write(content)
return item
def close_spider(self,spider):
print('我只会在爬虫结束的时候调用一次1!!')
self.fp.close()

settings.py:

BOT_NAME = 'firstBlood'

SPIDER_MODULES = ['firstBlood.spiders']
NEWSPIDER_MODULE = 'firstBlood.spiders'


# Crawl responsibly by identifying yourself (and your website) on the user-agent
USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'

# Obey robots.txt rules
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
#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/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 = {
# 'firstBlood.middlewares.FirstbloodSpiderMiddleware': 543,
#}

# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# 'firstBlood.middlewares.FirstbloodDownloaderMiddleware': 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 = {
'firstBlood.pipelines.FirstbloodPipeline': 300,#300表示的优先级,越小优先级越高
}

# 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'