CrawlSpider类是什么?
- 是Spider的一个子类
- 区别:
- Spider是获取到URL进行 手动发送请求 : yield scrapy.Request(url=new_url,callback=self.parse)
- 是通过提取器类:LinkExtractor,提前到页面所有符合条件的URL,然后用Rule类对符合条件的URL自动发送请求
- 创建CrawlSpider爬虫的命令:scrapy genspider -t crawl xxx(爬虫名称) www.xxxx.com(爬取的URL)
基于CrawlSpider创建的爬虫类,代码示例:
import scrapy
from scrapy.linkextractors import LinkExtractor #导入url提取器的类
from scrapy.spiders import CrawlSpider, Rule #Rule可用于自动发送请求
class XuexiSpider(CrawlSpider):
name = \'xuexi\'
allowed_domains = [\'www.xxx.com\']
start_urls = [\'http://www.xxx.com/\']
#LinkExtractor(allow=r\'Items/\') 该类的allow参数写入正则匹配规则,就会按照正则去响应信息中匹配URL,当然也有别的匹配规则,比如CSS
rules = (
#follow为True可以自动将所有响应信息的符合的规则的url都获取到,并发送请求
Rule(LinkExtractor(allow=r\'Items/\'), callback=\'parse_item\', follow=True),
)
def parse_item(self, response):
item = {}
#item[\'domain_id\'] = response.xpath(\'//input[@id=\"sid\"]/@value\').get()
#item[\'name\'] = response.xpath(\'//div[@id=\"name\"]\').get()
#item[\'description\'] = response.xpath(\'//div[@id=\"description\"]\').get()
return item
下面也一个案例,就以爬取阳光信息网为例,代码示例:
#1.爬虫文件.py代码示例:
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from sunPro.items import SunproItem,DetailItem
#需求:爬取sun网站中的编号,新闻标题,新闻内容,标号
class SunSpider(CrawlSpider):
name = \'sun\'
# allowed_domains = [\'www.xxx.com\']
start_urls = [\'http://wz.sun0769.com/index.php/question/questionType?type=4&page=\']
#链接提取器:根据指定规则(allow=\"正则\")进行指定链接的提取
link = LinkExtractor(allow=r\'type=4&page=\\d+\')
link_detail = LinkExtractor(allow=r\'question/\\d+/\\d+\\.shtml\')
rules = (
#规则解析器:将链接提取器提取到的链接进行指定规则(callback)的解析操作
Rule(link, callback=\'parse_item\', follow=True),
#follow=True:可以将链接提取器 继续作用到 连接提取器提取到的链接 所对应的页面中
Rule(link_detail,callback=\'parse_detail\')
)
#http://wz.sun0769.com/html/question/201907/421001.shtml
#http://wz.sun0769.com/html/question/201907/420987.shtml
#解析新闻编号和新闻的标题
#如下两个解析方法中是不可以实现请求传参!
#如法将两个解析方法解析的数据存储到同一个item中,可以以此存储到两个item
def parse_item(self, response):
#注意:xpath表达式中不可以出现tbody标签
tr_list = response.xpath(\'//*[@id=\"morelist\"]/div/table[2]//tr/td/table//tr\')
for tr in tr_list:
new_num = tr.xpath(\'./td[1]/text()\').extract_first()
new_title = tr.xpath(\'./td[2]/a[2]/@title\').extract_first()
item = SunproItem()
item[\'title\'] = new_title
item[\'new_num\'] = new_num
yield item
#解析新闻内容和新闻编号
def parse_detail(self,response):
new_id = response.xpath(\'/html/body/div[9]/table[1]//tr/td[2]/span[2]/text()\').extract_first()
new_content = response.xpath(\'/html/body/div[9]/table[2]//tr[1]//text()\').extract()
new_content = \'\'.join(new_content)
# print(new_id,new_content)
item = DetailItem()
item[\'content\'] = new_content
item[\'new_id\'] = new_id
yield item
#2.itmes.py代码示例:
#因为是不同页面的数据,又不能进行参数化,所有通过两个item类,来接收不同页面的解析数据
import scrapy
class SunproItem(scrapy.Item):
# define the fields for your item here like:
title = scrapy.Field()
new_num = scrapy.Field()
class DetailItem(scrapy.Item):
new_id = scrapy.Field()
content = scrapy.Field()
#3.pipeline.py代码示例:
#根据不同item的名字,来判断,数据来源于哪一个item
class SunproPipeline(object):
def process_item(self, item, spider):
#如何判定item的类型
#将数据写入数据库时,如何保证数据的一致性
if item.__class__.__name__ == \'DetailItem\':
print(item[\'new_id\'],item[\'content\'])
pass
else:
print(item[\'new_num\'],item[\'title\'])
return item
来源:https://www.cnblogs.com/zhiqianggege/p/16203841.html
本站部分图文来源于网络,如有侵权请联系删除。