zoukankan      html  css  js  c++  java
  • 爬虫8:Scrapy-取内容

    scrapy的实例都分了好几次来写了,因为平时要工作,而且总是遇到这样那样的问题,所以进度一直很慢

    写程序有的时候也是玄学,好好的程序总是莫名其妙的就不能运行,然后又莫名其妙的好了,很是奇葩,就像今天的问题

    搞了半天搞不好,还像程序员求救,最后什么都没干又自己好了

    不过程序员哥哥还是说得对,代码之前能运行那说明代码逻辑没问题,又确定了格式没问题,那剩下的多半是环境的问题了,写代码一定要多注意细节。

    因为之前在学selenium的时候,已经学过xpath的提取了,所以这个倒没费多大的功夫,直接上代码了

    from scrapy.spiders import Spider
    from scrapy.selector import Selector
    from tutorial.items import DmozItem
    
    class DmozSpider(Spider):
        name = "dmoz"
        allowed_domains = ["dmoz.org"]
        start_urls=[
            "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
            "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"
        ]
        def parse(self,response):
            sel = Selector(response)
            sites = sel.xpath('//div[@class="site-title"]/text()')
            items=[]
            for site in sites:
                item =DmozItem()
                item['title'] = site.extract()
                items.append(item)
            return items

    再接下来是存储内容了

    信息保存主要有四种:JSON, JSON lines, CSV, XML

    我们用json导出

    -o后面是文件名,-t后面是导出类型

    scrapy crawl dmoz -o axiba.json -t json  

    目前为止这个例子就学完了,下一步去研究项目的爬虫了

    嘿嘿

  • 相关阅读:
    Linux----------自动化运维工具-ansible
    Linux----------自动化运维
    Linux----------集群介绍
    Linux----------集群-LVS
    Linux----------zabbix监控-进阶
    Linux----------zabbix监控基础
    Linux----------mysql主从复制和基于GTID主从复制
    Linux----------mysql主从及高可用
    MongoDB笔记(四)基本管理命令
    MongoDB笔记(三)启动命令mongod的参数
  • 原文地址:https://www.cnblogs.com/ronyjay/p/6525865.html
Copyright © 2011-2022 走看看