初识scrap框架

1.爬虫的框架:

scrap :

An open source and collaborative framework for extracting the data you need from websites.
In a fast, simple, yet extensible way.
1.开源的爬虫框架
2.快速、简单、高效的方式

2.基本使用

1.创建一个项目
scrapy startproject test_scrapy

	1.项目目录:
		1.scrapy.cfg  【项目的配置文件】
		2.settings.py  【项目的配置文件】
		3. spiders/  【防止 爬虫代码的目录】
2.编写爬虫代码
	1.创建一个 爬虫代码
	scrapy genspider [options] <name> <domain>

	scrapy genspider python01 www.xxx.com

 '''
name:
    1.不能重复
    2.爬虫文件的名字
'''
name = 'python01'
'''
   scrapy 允许爬取的 url 
'''
allowed_domains = ['www.baidu.com']
'''
    scrapy 去爬取的 url 列表
'''
start_urls = ['http://www.baidu.com/','https://www.sougou.com']

3.启动爬虫项目
	1.启动命令
	scrapy runspider [options] <spider_file>

	scrapy runspider ./test_scrapy/spiders/python01.py

3.代码展示

import scrapy
# -*- coding:utf-8 -*-

class Python01Spider(scrapy.Spider):
    '''
    name:
        1.不能重复
        2.爬虫文件的名字
    '''
    name = 'python01'

    #allowed_domains = ['www.baidu.com']
    '''
        scrapy 去爬取的 url 列表
    '''
    start_urls = ['https://www.shicimingju.com/book/sanguoyanyi.html']

    def parse(self, response):


        #1.scrapy 数据解析 =》
        '''
            1.标签定位:
                返回 selector(xpath,data[xpath表达式 结果])
            2.数据解析
                返回 selector(xpath,data[xpath表达式 结果])
        '''
        li_list = response.xpath('//div[@class="book-mulu"]/ul/li')

        res= []
        for index,li in enumerate(li_list):
            a_text_get = li.xpath('./a/text()').get()

            a_text_get.encode('uf-8').decode('unicode_escape')

            dic={
                'id':index,
                'title':a_text_get
            }
            res.append(dic)

        return res




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值