admin 管理员组文章数量: 1087678
爬虫Scrapy框架学习(五)
本案例通过典型的scrapy框架Spider类展现了一个模板式的爬虫过程,请读者细细体会,此案例为必会项目,按照本博客配置,完美通过.本文还对item做了限制,编写item文件的主要作用是防止爬取数据存入子字典中的键值命名错误.
1.项目框架展示
2.爬取页面展示
3.yg.py文件
# -*- coding: utf-8 -*-
import scrapy
from yangguang.items import YangguangItemclass YgSpider(scrapy.Spider):name = 'yg'allowed_domains = ['sun0769.com']start_urls = ['.php/question/report?page=']def parse(self, response):#分组tr_list = response.xpath("//div[@class='greyframe']/table[2]/tr/td/table/tr")for tr in tr_list:item = YangguangItem() #实例化爬取的数据限制(防止命名错误)item["title"] = tr.xpath("./td[2]/a[@class='news14']/@title").extract_first()item["href"] = tr.xpath("./td[2]/a[@class='news14']/@href").extract_first()item["publish_date"] = tr.xpath("./td[last()]/text()").extract_first()#进去详情页yield scrapy.Request(item["href"],callback = self.parse_detail,meta = {"item":item},)#翻页next_url = response.xpath("//a[text() = '>']/@href").extract_first()if next_url is not None:#判断是否有下一页yield scrapy.Request(next_url,callback = self.parse)def parse_detail(self, response):"""处理详情页"""item = response.meta["item"]item["content"] = response.xpath("//td[@class = 'txt16_3']//text()").extract_first() #选取多个选项item["content_img"] = response.xpath("//div[@class = 'c1 text14_2']//img/@src").extract()item["content_img"] = ["/" + i for i in item["content_img"]] #列表推导式添加图片前面缺失的域名# print(item)yield item #输出到pipeline
4.item.py文件
# -*- coding: utf-8 -*-# Define here the models for your scraped items
#
# See documentation in:
# .htmlimport scrapyclass YangguangItem(scrapy.Item):# define the fields for your item here like:# name = scrapy.Field()title = scrapy.Field()href = scrapy.Field()publish_date = scrapy.Field()content_img = scrapy.Field()content = scrapy.Field()
5.pipeline.py文件
# -*- coding: utf-8 -*-# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: .html
import reclass YangguangPipeline(object):def open_spider(self, response): #在爬虫开始时打开一次和关闭时打开一次,而且只执行一次passdef close_spider(self, response):#但是如果在结束中统一保存文件,如果在程序中间出错了就不会保存已经爬取的数据了。passdef process_item(self, item, spider):item["content"] = self.process_content(item["content"])print(item)return itemdef process_content(self, content):#处理content内容,去除无关信息# contnet = [re.sub(r"\xa0|\s","",i) for i in content] #content为列表的情况# contnet = re.sub(r"\xa0","",content)# content = [i for i in content if len(i) > 0]#去除列表中的空字符串# print(content)content = re.sub("\xa0", "", content)#content为字符串return content
6.settings.py文件
# -*- coding: utf-8 -*-# Scrapy settings for yangguang project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
# .html
# .html
# .htmlBOT_NAME = 'yangguang'SPIDER_MODULES = ['yangguang.spiders'] #爬虫位置
NEWSPIDER_MODULE = 'yangguang.spiders' #新建爬虫的放置位置LOG_LEVEL = "WARNING"# Crawl responsibly by identifying yourself (and your website) on the user-agent
USER_AGENT = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'# Obey robots.txt rules
ROBOTSTXT_OBEY = True #默认情况下是否遵守robot协议# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32 #设置最大并发请求(默认是16个)# Configure a delay for requests for the same website (default: 0)
# See .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 #每一个id的最大并发请求数(几乎不会用到)# Disable cookies (enabled by default)
#COOKIES_ENABLED = False #cookie是否开启(默认是开启的)# 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 .html
#SPIDER_MIDDLEWARES = { #爬虫中间件
# 'yangguang.middlewares.YangguangSpiderMiddleware': 543,
#}# Enable or disable downloader middlewares
# See .html
#DOWNLOADER_MIDDLEWARES = { #下载中间件
# 'yangguang.middlewares.YangguangDownloaderMiddleware': 543,
#}# Enable or disable extensions
# See .html
#EXTENSIONS = {
# 'scrapy.extensions.telnet.TelnetConsole': None,
#}# Configure item pipelines
# See .html
ITEM_PIPELINES = {'yangguang.pipelines.YangguangPipeline': 300,
}# Enable and configure the AutoThrottle extension (disabled by default) #自动限速(一般不需要)
# See .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) #http缓存
# See .html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
7.结果展示
- content_img为空是因为在详情页并没有配图,有配图便可以爬取到,请读者自行查看网页源代码进行核验。
本文标签: 爬虫Scrapy框架学习(五)
版权声明:本文标题:爬虫Scrapy框架学习(五) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1687421317a100674.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论