Scrapy第一个项目

1. 安装scrapy

        pip install scrapy 

2. 创建项目的命令

        scrapy startproject <项目名>

- 示例:

        scrapy startproject myscrapyPro

3.创建爬虫

        cd 进到项目名 

                执行:

                        scrapy genspider <爬虫名字> <允许的域名>

                 示例:

                        scrapy genspider myspider www.xxx.com

4.完善爬虫

import scrapy

class mySpider(scrapy.Spider):  # 继承scrapy.spider
    # 爬虫名字 
    name = 'mypro' 
    # 允许爬取的范围
    allowed_domains = ['xxx.com'] 
    # 开始爬取的url地址
    start_urls = ['http://www.xxx.com']

    # 数据提取的方法,接受下载中间件传过来的response
    def parse(self, response): 
        # 直接打印response看下结果
        print(response)

        # scrapy的response对象可以直接进行xpath
        names = response.xpath('//div[@class="tea_con"]//li/div/h3/text()') 
        print(names)

        # 获取具体数据文本的方式如下
        # 分组
        li_list = response.xpath('//div[@class="tea_con"]//li') 
        for li in li_list:
            # 创建一个数据字典
            item = {}
            # 利用scrapy封装好的xpath选择器定位元素,并通过extract()或extract_first()来获取结果
            item['name'] = li.xpath('.//h3/text()').extract_first() # 名字
            item['level'] = li.xpath('.//h4/text()').extract_first() # 级别
            item['text'] = li.xpath('.//p/text()').extract_first() # 介绍
            print(item)

4.1 先打开setting文件中修改下UA

        

 4.2 日志级别改为

        LOG_LEVEL = "ERROR"  这样只显示报错信息

5. 终端执行运行命令

        scrapy crawl <爬虫名字>

        scrapy crawl <爬虫名字> -- nolog  不显示日志信息

5.1 提取数据,属性的方法

  1. response.xpath方法的返回结果是一个类似list的类型,其中包含的是selector对象,操作和列表一样,但是有一些额外的方法

  2. 额外方法extract():返回一个包含有字符串的列表

  3. 额外方法extract_first():返回列表中的第一个字符串,列表为空没有返回None

5.2 response响应对象的常用属性

        1.响应url

         print(response.url)

        2.请求url

        print(response.request.url)

        3.响应头

        print(response.headers)

        4.请求头

        print(response.request.headers)

        5.html

        print(response.body)

        6.响应状态码

        print(response.status)

6.保存数据

6.1 在命令行 scrapy可以直接保存 csv,json,xml

执行命令:

        scrapy crawl <爬虫名> -o <文件名>.csv 

6.2 管道存储

6.2.1 导入myproItem对象

import scrapy

# 导入myproItem
from mySpider.items import myproItem

 6.2.2 实例化myproItem对象 并提交到item

# 数据提取的方法,接受下载中间件传过来的response
def parse(self, response):
    # 获取具体数据文本的方式如下
    # 分组
    li_list = response.xpath('//div[@class="tea_con"]//li') 
    for li in li_list:
        # 实例化item对象
        item = myproItem()

        # 利用scrapy封装好的xpath选择器定位元素,并通过extract()或extract_first()来获取结果
        item['name'] = li.xpath('.//h3/text()').extract_first() # 名字
        item['level'] = li.xpath('.//h4/text()').extract_first() # 级别
        item['text'] = li.xpath('.//p/text()').extract_first() # 介绍
        
        # 提交item
        yield item

 6.2.3 在item类中定义相关属性

import scrapy


class ItcastproItem(scrapy.Item):
    # define the fields for your item here like:
    name = scrapy.Field()
    level = scrapy.Field()
    text = scrapy.Field()
    

6.3 在管道文件中定义方法

class ItcastproPipeline:
    
    def __init__(self):
        print("开始爬虫")
        self.f = open('123.txt',mode='w',encoding='utf-8')

    

    def process_item(self, item, spider):
        # 把itme对象转成str
        name = item['name']
        level = item['level']
        text = item['text']
        
        self.f.write(name)
        self.f.write('\n')
        self.f.write(level)
        self.f.write('\n')
        self.f.write(text)
        self.f.write('\n')
        
        return item
    
    

    def __del__(self):
        print("结束爬虫")
        self.f.close()

6.3.1 也可以这样写 (传入spider)

def open_spider(self,spider):
        # 打开文件
        self.f = open("")
    
    def process_item(self, item, spider):
        # 写入数据
        self.f.write()
        
        return item
    
    
    def close_spider(self,spider):
        # 关闭文件
        self.f.close()

6.4 启用管道

ITEM_PIPELINES = {
    'myspider.pipelines.ItcastPipeline': 400
}

6.5 执行scrapy crawl <爬虫名>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值