美文网首页Python爬虫网络爬虫(武装你的爬虫)Python 爬虫专栏
爬虫练手:使用IP代理池,爬取微信文章信息

爬虫练手:使用IP代理池,爬取微信文章信息

作者: BlueCat2016 | 来源:发表于2017-01-02 22:10 被阅读1337次

    爬取对象:利用搜狗的微信搜索功能,爬取微信文章信息。
    网址:http://weixin.sogou.com/
    python版本:python3.5
    注:因搜狗防爬机制较为严格,国内的免费代理IP一般均无效,所以本案例中使用的代理IP均无效,本博文只是为了记录和演示代理IP池的用法。

    源代码

    items.py

    # -*- 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 WeixinItem(scrapy.Item):
        # define the fields for your item here like:
        # name = scrapy.Field()
        #标题
        title = scrapy.Field()
        #链接
        link = scrapy.Field()
        #简介
        desc = scrapy.Field()
    

    weixin_article.py

    # -*- coding: utf-8 -*-
    import scrapy
    from scrapy.http import Request
    from weixin.items import WeixinItem
    
    class WeixinArticleSpider(scrapy.Spider):
        name = "weixin_article"
        allowed_domains = ["sogou.com"]
        start_urls = ['http://sogou.com/']
    
        def parse(self, response):
            #构造关键词
            key='python'
            for i in range(1,3):
    
                thispage = "http://weixin.sogou.com/weixin?query=python&_sug_type_=&sut=861&lkt=7%2C1483249474246%2C1483249475122&_sug_=y&type=2&sst0=1483249475225&page=" + str(i) + "&ie=utf8&w=01019900&dr=1"
                print("第" + str(i) + "次爬取,网址为:" + thispage)
                yield Request(url=thispage,callback=self.page)
    
        def page(self,response):
            print("在处理搜索列表页,正在爬的列表页:" + response.url)
            item = WeixinItem()
            item["title"] = response.xpath("//div[@class='txt-box']/h3/a/text()").extract()
            item["link"] = response.xpath("//div[@class='txt-box']/h3/a/@href").extract()
            item["desc"] = response.xpath("//p[@class='txt-info']/text()").extract()
    
            yield item
    

    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
    
    
    class WeixinPipeline(object):
        def process_item(self, item, spider):
            for i in range(0,len(item["title"])):
                print("第%s篇文章的信息是:"%i)
                print(item["title"][i])
                print(item["link"][i])
                print(item["desc"][i])
                print("------------------")
            return item
    

    middlewares.py

    # -*- coding: utf-8 -*-
    
    # Define here the models for your spider middleware
    #
    # See documentation in:
    # http://doc.scrapy.org/en/latest/topics/spider-middleware.html
    
    from scrapy import signals
    import random
    from weixin.settings import IPPOOL
    #导入官方代理IP的一个模块
    from scrapy.contrib.downloadermiddleware.httpproxy import HttpProxyMiddleware
    
    class IPPOOLS(HttpProxyMiddleware):
        def __init__(self,ip=''):
            self.ip = ip
    
        def process_request(self, request, spider):
            thisip = random.choice(IPPOOL)
            print("当前使用的IP是:" + thisip["ipaddr"])
            try:
                request.meta["proxy"] = "http://" + thisip["ipaddr"]
            except Exception as e:
                pass
    
    
    class WeixinSpiderMiddleware(object):
        # Not all methods need to be defined. If a method is not defined,
        # scrapy acts as if the spider middleware does not modify the
        # passed objects.
    
        @classmethod
        def from_crawler(cls, crawler):
            # This method is used by Scrapy to create your spiders.
            s = cls()
            crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
            return s
    
        def process_spider_input(response, spider):
            # Called for each response that goes through the spider
            # middleware and into the spider.
    
            # Should return None or raise an exception.
            return None
    
        def process_spider_output(response, result, spider):
            # Called with the results returned from the Spider, after
            # it has processed the response.
    
            # Must return an iterable of Request, dict or Item objects.
            for i in result:
                yield i
    
        def process_spider_exception(response, exception, spider):
            # Called when a spider or process_spider_input() method
            # (from other spider middleware) raises an exception.
    
            # Should return either None or an iterable of Response, dict
            # or Item objects.
            pass
    
        def process_start_requests(start_requests, spider):
            # Called with the start requests of the spider, and works
            # similarly to the process_spider_output() method, except
            # that it doesn’t have a response associated.
    
            # Must return only requests (not items).
            for r in start_requests:
                yield r
    
        def spider_opened(self, spider):
            spider.logger.info('Spider opened: %s' % spider.name)
    

    settings.py

    # -*- coding: utf-8 -*-
    
    # Scrapy settings for weixin 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 = 'weixin'
    
    SPIDER_MODULES = ['weixin.spiders']
    NEWSPIDER_MODULE = 'weixin.spiders'
    
    
    # Crawl responsibly by identifying yourself (and your website) on the user-agent
    #USER_AGENT = 'weixin (+http://www.yourdomain.com)'
    
    # Obey robots.txt rules
    ROBOTSTXT_OBEY = False
    
    # 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 = {
    #    'weixin.middlewares.WeixinSpiderMiddleware': 543,
    #}
    
    # Enable or disable downloader middlewares
    # See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
    # DOWNLOADER_MIDDLEWARES = {
    #    # 'weixin.middlewares.MyCustomDownloaderMiddleware': 543,
    #     'scrapy.contrib.downloadermiddleware.httpproxy.HttpProxyMiddleware':123,
    #     'weixin.middlewares.IPPOOLS':125,
    # }
    
    # 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 = {
       'weixin.pipelines.WeixinPipeline': 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'
    
    IPPOOL = [
        {"ipaddr":"110.73.33.207:6673"},
        {"ipaddr":"122.89.138.20:6675"},
        {"ipaddr":"110.72.20.245:6673"},
    ]
    

    相关文章

      网友评论

        本文标题:爬虫练手:使用IP代理池,爬取微信文章信息

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