美文网首页
凤凰网分类爬虫

凤凰网分类爬虫

作者: Py_Explorer | 来源:发表于2017-11-09 20:16 被阅读0次

    1. pycharm开发工具+python2.7+scrapy框架

    2.项目开发

    2.1 创建项目

    scrapy startproject Ifeng

    image.png

    2.2 写自己需要的参数,在items文件里面写

    # -*- coding: utf-8 -*-
    
    # Define here the models for your scraped items
    #
    # See documentation in:
    # http://doc.scrapy.org/en/latest/topics/items.html
    
    import scrapy
    
    
    class IfengdataItem(scrapy.Item):
        # define the fields for your item here like:
        # name = scrapy.Field()
        # 大类的标题 和 url
        parentTitle = scrapy.Field()
        parentUrls = scrapy.Field()
    
        # 小类的标题 和 子url
        subTitle = scrapy.Field()
        subUrls = scrapy.Field()
    
        # 小类目录存储路径
        subFilename = scrapy.Field()
    
        # 小类下的子链接
        sonUrls = scrapy.Field()
    
        # 文章标题和内容
        head = scrapy.Field()
        content = scrapy.Field()
        pass
    

    2.2 在spider子目录下新建爬虫开始文件,我这里命名为ifeng.py

    #coding=utf-8
    import scrapy
    from ifengdata.items import IfengdataItem
    import os
    class ifengdata(scrapy.Spider):
        name='ifeng'
        allowed_domains = ["ifeng.com"]
        start_urls = [
            "http://www.ifeng.com/daohang/"
        ]
        def parse(self, response):
    
            items=[]
            #所有的大标题和url
            parentUrls = response.xpath('//div[@class="col3"]/h2/a/@href').extract()
            parentTitle = response.xpath('//div[@class="col3"]/h2/a/text()').extract()
    
            # 所有小类的ur 和 标题
            subUrls = response.xpath('//div[@class="col3"]/div/div/div/div/ul/li/a/@href').extract()
            subTitle= response.xpath('//div[@class="col3"]/div/div/div/div/ul/li/a/text()').extract()
            for i in range(0,len(parentTitle)):
                # 指定大类目录的路径和目录名
                parentFilename = "./Data/" + parentTitle[i]
    
                # 如果目录不存在,则创建目录
                if (not os.path.exists(parentFilename)):
                    os.makedirs(parentFilename)
                for j in range(0,len(subTitle)):
                    item = IfengdataItem()
                    item['parentUrls']=parentUrls[i]
                    item['parentTitle']=parentTitle[i]
                    # yield item
                    # 检查小类的url是否以同类别大类url开头,如果是返回True (sports.sina.com.cn 和 sports.sina.com.cn/nba)
                    if_belong = subUrls[j].startswith(item['parentUrls'])
                    # 如果属于本大类,将存储目录放在本大类目录下
                    if (if_belong):
                        subFilename = parentFilename + '/' + subTitle[j]
                        # 如果目录不存在,则创建目录
                        if (not os.path.exists(subFilename)):
                            os.makedirs(subFilename)
    
                        # 存储 小类url、title和filename字段数据
                        item['subUrls'] = subUrls[j]
                        item['subTitle'] = subTitle[j]
                        item['subFilename'] = subFilename
                        items.append(item)
                        # 发送每个小类url的Request请求,得到Response连同包含meta数据 一同交给回调函数 second_parse 方法处理
            for item in items:
                yield scrapy.Request(url=item['subUrls'], meta={'meta_1': item}, callback=self.second_parse)
    
        def second_parse(self,response):
            # 提取每次Response的meta数据
            meta_1 = response.meta['meta_1']
            # 取出小类里所有子链接
            # 取出小类里所有子链接
            sonUrls = response.xpath('//a/@href').extract()
    
            items = []
            for i in range(0, len(sonUrls)):
                # 检查每个链接是否以大类url开头、以.shtml结尾,如果是返回True
                if_belong =sonUrls[i].startswith(meta_1['parentUrls'])
    
                # 如果属于本大类,获取字段值放在同一个item下便于传输
                if (if_belong):
                    item = IfengdataItem()
                    item['parentTitle'] = meta_1['parentTitle']
                    item['parentUrls'] = meta_1['parentUrls']
                    item['subUrls'] = meta_1['subUrls']
                    item['subTitle'] = meta_1['subTitle']
                    item['subFilename'] = meta_1['subFilename']
                    item['sonUrls'] = sonUrls[i]
                    items.append(item)
            # 发送每个小类下子链接url的Request请求,得到Response后连同包含meta数据 一同交给回调函数 detail_parse 方法处理
            for item in items:
                yield scrapy.Request(url=item['sonUrls'], meta={'meta_2': item}, callback=self.detail_parse)
    
                # 数据解析方法,获取文章标题和内容
    
        def detail_parse(self, response):
            item = response.meta['meta_2']
            content = ""
            head = response.xpath('//h1[@id=\"artical_topic\"]/text()').extract()
            content_list = response.xpath('//div[@id=\"main_content\"]/p/text()').extract()
            # 将p标签里的文本内容合并到一起
            for content_one in content_list:
                content += content_one
            if head!='':
                item['head'] = head[0]
                print(item['head'])
            else :
                item['head']=1
            item['content'] = content
    
            yield item
    

    2.3配置管道pipelines.py

    # -*- coding: utf-8 -*-
    
    # Define your item pipelines here
    #
    # Don't forget to add your pipeline to the ITEM_PIPELINES setting
    # See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
    
    # import pymongo
    import pymysql
    from ifengdata import settings
    class IfengdataPipeline(object):
        def __init__(self):
            # host='127.0.0.1'
            # port=27017
            # client=pymongo.MongoClient(host=host,port=port)
            # dbname = 'ifeng'
            #
            # # pymongo.MongoClient(host, port) 创建MongoDB链接
            # # 指向指定的数据库
            # mdb = client[dbname]
            # # 获取数据库里存放数据的表名
            # self.post = mdb['ifengdata1']
            # 获取setting主机名、端口号和数据库名
            # host = settings['MONGODB_HOST']
            # host='127.0.0.1'
            # port = settings['MONGODB_PORT']
            # port=27017
            # dbname = settings['MONGODB_DBNAME']
            # dbname='IFeng'
            # pymongo.MongoClient(host, port) 创建MongoDB链接
            # client = pymongo.MongoClient(host=host, port=port)
            # 指向指定的数据库
            # mdb = client[dbname]
            # 获取数据库里存放数据的表名
            # self.post = mdb[settings['MONGODB_DOCNAME']]
            # self.post= mdb['IFengData']
            self.conn = pymysql.connect(
                host='localhost',
                port=3306,
                user='root',
                password='root',
                db='test',
                charset='utf8',
            )
            self.cursor=self.conn.cursor()
        def process_item(self, item, spider):
            item = dict(item)
            sql = 'select * from ifengdata4 WHERE mulu=%s'
            par =[item['parentTitle']]
            name = self.cursor.execute(sql,par)
            self.conn.commit()
            if name:
                pass
            else:
                sql1='insert into ifengdata4(id,mulu) VALUES(null,%s)'
                params=[item['parentTitle']]
                self.cursor.execute(sql1,params)
                self.conn.commit()
            sql4 = 'select * from ifengdata5 WHERE zimulu=%s'
            par4 = [item['subTitle']]
            name = self.cursor.execute(sql4, par4)
            self.conn.commit()
            if name:
                pass
            else:
                sql2 = 'insert into ifengdata5(id,zimulu) VALUES(null,%s)'
                params2=[item['subTitle']]
                self.cursor.execute(sql2, params2)
                self.conn.commit()
            sql3 = 'insert into ifengdata6(id,title) VALUES(null,%s)'
            params3 = [item['head']]
            self.cursor.execute(sql3, params3)
            self.conn.commit()
            return item
    

    2.4设置settings.py文件

    # -*- coding: utf-8 -*-
    
    # Scrapy settings for ifengdata project
    #
    # For simplicity, this file contains only settings considered important or
    # commonly used. You can find more settings consulting the documentation:
    #
    #     http://doc.scrapy.org/en/latest/topics/settings.html
    #     http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
    #     http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
    
    BOT_NAME = 'ifengdata'
    
    SPIDER_MODULES = ['ifengdata.spiders']
    NEWSPIDER_MODULE = 'ifengdata.spiders'
    # Crawl responsibly by identifying yourself (and your website) on the user-agent
    #USER_AGENT = 'ifengdata (+http://www.yourdomain.com)'
    USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; WOW64)'
    # Obey robots.txt rules
    ROBOTSTXT_OBEY = True
    
    # Configure maximum concurrent requests performed by Scrapy (default: 16)
    #CONCURRENT_REQUESTS = 32
    
    # Configure a delay for requests for the same website (default: 0)
    # See http://scrapy.readthedocs.org/en/latest/topics/settings.html#download-delay
    # See also autothrottle settings and docs
    #DOWNLOAD_DELAY = 3
    # The download delay setting will honor only one of:
    #CONCURRENT_REQUESTS_PER_DOMAIN = 16
    #CONCURRENT_REQUESTS_PER_IP = 16
    
    # Disable cookies (enabled by default)
    #COOKIES_ENABLED = False
    
    # Disable Telnet Console (enabled by default)
    #TELNETCONSOLE_ENABLED = False
    
    # Override the default request headers:
    #DEFAULT_REQUEST_HEADERS = {
    #   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    #   'Accept-Language': 'en',
    #}
    
    # Enable or disable spider middlewares
    # See http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
    #SPIDER_MIDDLEWARES = {
    #    'ifengdata.middlewares.IfengdataSpiderMiddleware': 543,
    #}
    
    # Enable or disable downloader middlewares
    # See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
    #DOWNLOADER_MIDDLEWARES = {
    #    'ifengdata.middlewares.MyCustomDownloaderMiddleware': 543,
    #}
    
    # Enable or disable extensions
    # See http://scrapy.readthedocs.org/en/latest/topics/extensions.html
    #EXTENSIONS = {
    #    'scrapy.extensions.telnet.TelnetConsole': None,
    #}
    
    # Configure item pipelines
    # See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html
    ITEM_PIPELINES = {
       'ifengdata.pipelines.IfengdataPipeline': 300,
    }
    
    # Enable and configure the AutoThrottle extension (disabled by default)
    # See http://doc.scrapy.org/en/latest/topics/autothrottle.html
    #AUTOTHROTTLE_ENABLED = True
    # The initial download delay
    #AUTOTHROTTLE_START_DELAY = 5
    # The maximum download delay to be set in case of high latencies
    #AUTOTHROTTLE_MAX_DELAY = 60
    # The average number of requests Scrapy should be sending in parallel to
    # each remote server
    #AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
    # Enable showing throttling stats for every response received:
    #AUTOTHROTTLE_DEBUG = False
    
    # Enable and configure HTTP caching (disabled by default)
    # See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
    #HTTPCACHE_ENABLED = True
    #HTTPCACHE_EXPIRATION_SECS = 0
    #HTTPCACHE_DIR = 'httpcache'
    #HTTPCACHE_IGNORE_HTTP_CODES = []
    #HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
    # MONGODB 主机环回地址127.0.0.1
    # MONGODB_HOST = '127.0.0.1'
    # 端口号,默认是27017
    # MONGODB_PORT = 27017
    # 设置数据库名称
    # MONGODB_DBNAME = 'IFeng'
    # 存放本次数据的表名称
    # MONGODB_DOCNAME = 'IFengData'
    
    DOWNLOAD_DELAY = 1
    
    REDIS_HOST = "192.168.13.23"
    REDIS_PORT = 6379
    

    可以试一下哦,有问题请留言

    相关文章

      网友评论

          本文标题:凤凰网分类爬虫

          本文链接:https://www.haomeiwen.com/subject/ftbmmxtx.html