• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

scrapy 的入门使用级详细

武飞扬头像
ajaxPost293614
帮助2

记录一下scrapy的安装和实践操作的流程

1.安装

pip install Scrapy

2.创建scrapy项目 

project是蜘蛛名字

scrapy startproject project

3.创建scrapy   res.py  蜘蛛文件  

保存在spiders目录下

4.查看是否可正常访问网站

scrapy shell http://lab.scrapyd.cn

看到response 返回200 说明此网站支持爬虫 

学新通

5.命令行测试 dom 元素内容

学新通

查找元素:我们要查找文案、作者、标签

可以看到下面的图片中展示了此页面的dom元素结构,所有的文字列表都保存在idmaindiv元素中,我们将其提取出来

学新通

  1.  
    // 执行代码
  2.  
    response.xpath('//div[@id="main"]').extract()

 得到以下结果,“”注意“” 这里得出来的是一个数组,为id为main标签下的所有数据

学新通

按照此方法 我们就能依次得到文案、作者、标签、页码

  1.  
    // 文案 text 类型str
  2.  
    response.xpath('span[@class="text"]/text()').extract_first()
  3.  
     
  4.  
    // 作者 text 类型str
  5.  
    response.xpath('span/small/text()').extract_first()
  6.  
     
  7.  
    // 标签 text 类型arr
  8.  
    response.xpath('div/a/text()').extract()
  9.  
     
  10.  
    // 页码
  11.  
    response.xpath('//ol[@class="page-navigator"]/li/a'[1].xpath('./@href').extract_first()

6.编写代码

1.items.py

  1.  
    import scrapy
  2.  
     
  3.  
    class resItem(scrapy.Item):
  4.  
    # define the fields for your item here like:
  5.  
    # name = scrapy.Field()
  6.  
    # list = response.xpath('//div[@id="main"]/div')
  7.  
     
  8.  
    # 短文 str list[0].xpath('span[@class="text"]/text()').extract_first()
  9.  
    short = scrapy.Field()
  10.  
     
  11.  
    # 作者 str list[0].xpath('span/small/text()').extract_first()
  12.  
    author = scrapy.Field()
  13.  
     
  14.  
    # 标签 arr list[0].xpath('div/a/text()').extract()
  15.  
    tags = scrapy.Field()
学新通

2.编写spiders目录下的蜘蛛文件 res.py

  1.  
    import scrapy
  2.  
     
  3.  
    from tutorial.items import resItem
  4.  
     
  5.  
    # 声明页码
  6.  
    pageNum = 0
  7.  
     
  8.  
     
  9.  
    class itemSpider(scrapy.Spider):
  10.  
    # 定义spider的名字
  11.  
    name = 'listSpider'
  12.  
     
  13.  
    # 从哪个页面开始
  14.  
    start_urls = ['http://lab.scrapyd.cn']
  15.  
     
  16.  
    def parse(self, response):
  17.  
    # 声明全局变量
  18.  
    global pageNum
  19.  
     
  20.  
    list = response.xpath('//div[@id="main"]/div')
  21.  
     
  22.  
    for v in list:
  23.  
    item = resItem()
  24.  
    # 短文 str
  25.  
    short = v.xpath(
  26.  
    'span[@class="text"]/text()').extract_first()
  27.  
    item['short'] = v.xpath(
  28.  
    'span[@class="text"]/text()').extract_first()
  29.  
     
  30.  
    # 作者 str
  31.  
    autor = v.xpath('span/small/text()').extract_first()
  32.  
    item['author'] = v.xpath('span/small/text()').extract_first()
  33.  
     
  34.  
    # 标签 arr
  35.  
    tag = (v.xpath('div/a/text()').extract())
  36.  
    tags = ','.join(tag)
  37.  
    item['tags'] = ','.join(tag) # 数组转换为字符串
  38.  
     
  39.  
    fileName = '%s-语录.txt' % autor # 定义文件名,如:木心-语录.txt
  40.  
     
  41.  
    with open(fileName, "a ") as f: # 不同人的名言保存在不同的txt文档,“a ”以追加的形式
  42.  
    f.write(short)
  43.  
    f.write('\n') # ‘\n’ 表示换行
  44.  
    f.write('标签:' tags)
  45.  
    f.write('\n-------\n')
  46.  
    f.close()
  47.  
     
  48.  
    yield item
  49.  
     
  50.  
    pageNum = 1
  51.  
    if pageNum <= 5:
  52.  
    # 获取下一页的链接
  53.  
    new_link = response.xpath(
  54.  
    '//ol[@class="page-navigator"]/li/a')[pageNum].xpath('./@href').extract_first()
  55.  
    # 再次请求下一个页面
  56.  
    yield scrapy.Request(new_link, callback=self.parse, dont_filter=True)
  57.  
    print('pageNum:', pageNum)
学新通

3.编写pipelines.py 文件 (该文件负责将爬取到的信息写入(数据库,设备,控制台等))

  1.  
    class TutorialPipeline:
  2.  
     
  3.  
    # 此处的item 就是蜘蛛 yield 的item
  4.  
    def process_item(self, item, spider):
  5.  
     
  6.  
    print('short',item['short'])
  7.  
    print('author',item['author'])
  8.  
    print('tags',item['tags'])
  9.  
     
  10.  
    # return item

4.修改settings.py文件

学新通

 7.执行爬虫操作

进入蜘蛛文件夹下执行命令

scrapy crawl listSpider

得到保存的文件说明成功爬虫

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcehia
系列文章
更多 icon
同类精品
更多 icon
继续加载