scrapy学习笔记(一)

1 篇文章 0 订阅
1 篇文章 0 订阅

一、安装scrapy(pip  install  scrapy)

 二、在python  shell中导入scrapy

    Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
                  >>> import scrapy
                >>> scrapy.version_info
               (1, 7, 3)
               >>> 

三、在dos下创建第一个爬虫项目example

C:\Users\Administrator>scrapy
Scrapy 1.7.3 - no active project

Usage:
  scrapy <command> [options] [args]

Available commands:
  bench         Run quick benchmark test
  fetch         Fetch a URL using the Scrapy downloader
  genspider     Generate new spider using pre-defined templates
  runspider     Run a self-contained spider (without creating a project)
  settings      Get settings values
  shell         Interactive scraping console
  startproject  Create new project
  version       Print Scrapy version
  view          Open URL in browser, as seen by Scrapy

  [ more ]      More commands available when run from project directory

Use "scrapy <command> -h" to see more info about a command

C:\Users\Administrator>scrapy  startproject  example
New Scrapy project 'example', using template directory 'd:\python\python37\lib\s
ite-packages\scrapy\templates\project', created in:
    C:\Users\Administrator\example

You can start your first spider with:
    cd example
    scrapy genspider example example.com

四、分析目标网站源码http://books.toscrape.com/

五、第一个爬虫存到 C:\Users\Administrator\example\example\spiders\book_spider.py


import scrapy
class BooksSpider(scrapy.Spider):
    # 每一个爬虫的唯一标识
     name = "books"

     # 定义爬虫爬取的起始点,起始点可以是多个,这里只有一个
     start_urls = ['http://books.toscrape.com/']
     def parse(self, response):
         # 提取数据
         # 每一本书的信息在<article class="product_pod">中,我们使用
         # css()方法找到所有这样的article 元素,并依次迭代
         for book in response.css('article.product_pod'):
             # 书名信息在article > h3 > a 元素的title属性里
             # 例如: <a title="A Light in the Attic">A Light in the ...</a>
             name = book.xpath('./h3/a/@title').extract_first()

             # 书价信息在 <p class="price_color">的TEXT中。
             # 例如: <p class="price_color">£51.77</p>
             price = book.css('p.price_color::text').extract_first()
             yield {
                 'name': name,
                 'price': price,
                 } 
         # 提取链接
         # 下一页的url 在ul.pager > li.next > a 里面
         # 例如: <li class="next"><a href="catalogue/page-2.html">next</a></li>
         next_url = response.css('ul.pager li.next a::attr(href)').extract_first()
         if next_url:
             # 如果找到下一页的URL,得到绝对路径,构造新的Request 对象
             next_url = response.urljoin(next_url)
             yield scrapy.Request(next_url, callback=self.parse)
六、到目录 C:\Users\Administrator\example下运行scrapy crawl books -o books.csv

C:\Users\Administrator\example>scrapy crawl books -o books.csv

生成如下books.csv文件

nameprice
A Light in the Attic£51.77
Tipping the Velvet£53.74
Soumission£50.10
Sharp Objects£47.82
Sapiens: A Brief History of Humankind£54.23
The Requiem Red£22.65
The Dirty Little Secrets of Getting Your Dream Job£33.34

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值