创建一个工程 : scrapy startproject xxPro
在spiders中创建一个爬虫文件
-- scrapy genspider spiderName www.xxx.com
执行工程:
--- scrapy crawl spiderName
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
?
# 存储文件
import pymysql
class AaproPipeline:
# 专门用来处理item类型的对象
# 该方法可以接受爬虫文件提交过来的item对象
# 该方法每接收到一个item就会被调用一次
fp=None
# 该方法的只需要调用一次
def open_spider(self,spider):
print(‘开始爬虫....‘)
self.fp=open(‘./t.txt‘,‘w‘,encoding=‘utf-8‘)
def process_item(self, item, spider):
tit = item[‘tit‘]
cont = item[‘cont‘]
self.fp.write(tit+‘:‘+cont+‘\n‘)
return item # 这个return item 反回的值会传入下一个执行的管道类
def close_spider(self,spider):
self.fp.close()
?
# 存储数据库
class mysqlPileLine(object):
coon=None
cursor = None
def open_spider(self,spider):
self.coon=pymysql.Connect(host=‘127.0.0.1‘,port=3306,user=‘root‘,password=‘123456‘,db=‘tet‘,charset=‘utf8‘)
def process_item(self,item, spider):
self.cursor = self.coon.cursor()
try:
self.cursor.execute(‘insert into qiub values("%s","%s")‘%(item["tit"],item["cont"]))
self.coon.commit()
except Exception as e:
print(e)
self.coon.rollback()
return item # 传给下一个执行的管道类
def close_spider(self,spider):
self.cursor.close()
self.coon.close()
?
settings.py 文件
# Scrapy settings for aaPro 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
?
BOT_NAME = ‘aaPro‘
?
SPIDER_MODULES = [‘aaPro.spiders‘]
NEWSPIDER_MODULE = ‘aaPro.spiders‘
?
?
# Crawl responsibly by identifying yourself (and your website) on the user-agent
# UA 伪装
USER_AGENT = ‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36‘
?
# Obey robots.txt rules
ROBOTSTXT_OBEY = False
?
LOG_LEVEL=‘ERROR‘
# 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 = {
# ‘aaPro.middlewares.AaproSpiderMiddleware‘: 543,
#}
?
# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# ‘aaPro.middlewares.AaproDownloaderMiddleware‘: 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 = {
# 300 表示的是优先级,数值越小优先级越高
‘aaPro.pipelines.AaproPipeline‘: 300,
‘aaPro.pipelines.mysqlPileLine‘: 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‘
?
爬虫文件.py
# Scrapy settings for aaPro 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
?
BOT_NAME = ‘aaPro‘
?
SPIDER_MODULES = [‘aaPro.spiders‘]
NEWSPIDER_MODULE = ‘aaPro.spiders‘
?
?
# Crawl responsibly by identifying yourself (and your website) on the user-agent
# UA 伪装
USER_AGENT = ‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36‘
?
# Obey robots.txt rules
ROBOTSTXT_OBEY = False
?
LOG_LEVEL=‘ERROR‘
# 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 = {
# ‘aaPro.middlewares.AaproSpiderMiddleware‘: 543,
#}
?
# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# ‘aaPro.middlewares.AaproDownloaderMiddleware‘: 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 = {
# 300 表示的是优先级,数值越小优先级越高
‘aaPro.pipelines.AaproPipeline‘: 300,
‘aaPro.pipelines.mysqlPileLine‘: 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‘
?
items.py
?
# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html
?
import scrapy
?
?
class AaproItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
tit = scrapy.Field()
cont = scrapy.Field()
?
原文:https://www.cnblogs.com/thaimj1314520/p/14696856.html