Scrapy教程

基础

1、创建一个项目

	scrapy startproject mySpider

2、新建一个爬虫

2、新建一个爬虫

scrapy genspiders spiders 
import scrapy
class SpidersSpider(scrapy.Spider):
    name = 'spiders'  # 爬虫名
    allowed_domains = ['itcast.cn']  # 允许爬虫的范围
    start_urls = ['http://itcast.cn/']  # 最开始请求的url的地址

    def parse(self, response):
          # 处理start_urls 地址对应的响应
        li_list = response.xpath('//div[@class="tea_con"]')
        for li in li_list:
            item = {
   }
            item['name'] = li.xpath(".//h3/text()").extract_first()
            item['title'] = li.xpath(".//h4/text()").extract_first()
        # res = response.xpath('//div[@class="tea_con"]//h3/text()').extract_first()
        # print(res)
        yield item  # 把列表传到piplines中
注:xpath写错会默认给提供None值

3、启动爬虫

3、启动爬虫

scrapy crwal spiders

在/settings下的设置里面

LOG_LEVEL = "WARNING"
控制台只显示warning以上水平的信息

4、pipline[管道]处理

4、pipline[管道]处理

/parse()下:
yield item  # 把列表传到piplines中
首先:settings中把注释的piplines取消注释

/settings下
ITEM_PIPELINES = {
   
   'myspider.pipelines.MyspiderPipeline': 300,  # 数据越小,越先执行
   'myspider.pipelines.MyspiderPipeline1': 301,  # 数据越小,越先执行
}
/piplines下
定义两个pipline类 
class MyspiderPipeline:
    def process_item(self, item, spider):
        print(item)
        item["hello"] = 'word'
        return item

class MyspiderPipeline1:
    def process_item(self, item, spider):
        print(item)
        return item
   
 执行结果:
{
   'name': '黄老师', 'title': '高级讲师'}
{
   'name': '黄老师', 'title': '高级讲师', 'hello': 'word'}

5、如何区别多个爬虫的pipline

5、如何区别多个爬虫的pipline

方式一:
 def parse(self, response):
    item = {
   }
    item["come_from"] = 'itcast'

class MyspiderPipeline:
    def process_item(self, item, spider):
        if item['come_from'] == 'itcast':
            print(item)
            item["hello"] = 'word'
            return item

方式二【推荐】:
class MyspiderPipeline:
    def process_item(self, item, spider):
        if spider.name == 'itcast':

6、logging输出

6、logging输出

import logging
logger = logging.getLogger(__name__)  # 能输出当前日志的输出

/setting下
LOG_LEVEL = "WARNING"
LOG_FILE = "./log.log"  #  将日志保存到本地

7、logging 非scrapy 输出

7、logging   非scrapy 输出

import logging
logging.basicConfig(level=logging.DEBUG,
          format='%(levelname)s %(filename)s '
              '%(message)s'
              ' - %(asctime)s', datefmt='[%d/%b/%Y %H:%M:%S]',
          filename='./loggmsg.log', filemode="a")
logger = logging.getLogger(__name__)

8、实现翻页

8、实现翻页
next_page_url = response.xpath("//a[text()='下一页']/@href).extract()

while len(next_page_url)>0:
	yield scrapy.Request(next_page_url, callback=self.parse)

9、yield scrapy.Request()使用介绍

#9yield scrapy.Request()使用介绍

yield scrapy.Request(url, 
callback
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值