幽灵王子 發表於 2016-5-31 09:58:00

Scrapy爬取美女图片第三集 代理ip(下)

<p>  <span style="font-size: 18px">这是我的公众号获取原创保护的首篇文章,原创的肯定将支持我继续前行。现在写这篇文章的时间是晚上11:30,写完就回寝室休息了,希望更多的朋友与我一起同行(<span style="color: rgba(255, 0, 0, 1)">当然需要一个善良的妹子的救济</span>)。<span style="font-size: 14pt">(我的新书<strong>《Python爬虫开发与项目实战》</strong>出版了,大家可以看一下<strong>样章</strong>)</span></span></p>
<p><span style="font-size: 18px">&nbsp; &nbsp; &nbsp; 好了,废话不多说,咱们进入今天的主题。上一篇咱们讲解了<span style="color: rgba(255, 0, 0, 1)">代理ip上篇</span>,本篇咱们继续讲解<span style="color: rgba(255, 0, 0, 1)">代理ip</span>。这一篇是上一篇的<span style="color: rgba(255, 0, 0, 1)">扩展</span>和<span style="color: rgba(255, 0, 0, 1)">优化</span>,主要的<span style="color: rgba(255, 0, 0, 1)">改动</span>是使用<span style="color: rgba(255, 0, 0, 1)">scrapy</span>来进行爬取代理ip,同时演示在scrapy框架中怎么<span style="color: rgba(255, 0, 0, 1)">使用mongodb数据库</span>,最后使用<span style="color: rgba(255, 0, 0, 1)">多线程批量验证</span>代理ip的合理性,大大加快了速度。</span></p>
<p><img src="http://images2015.cnblogs.com/blog/899843/201604/899843-20160424205256491-2076103008.jpg"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>  <span style="font-size: 18px">这次我选择的依然是<span style="color: rgba(255, 0, 0, 1)">http://www.xicidaili.com/nn/</span>,我之后打算做一个大的代理ip池,方便之后<span style="color: rgba(255, 0, 0, 1)">做分布式爬虫</span>。</span></p>
<p>&nbsp;</p>
<p><img src="http://images2015.cnblogs.com/blog/899843/201605/899843-20160522172556701-954432988.jpg"></p>
<p>&nbsp;</p>
<p>&nbsp;  <span style="font-size: 18px">使用f<span style="color: rgba(255, 0, 0, 1)">irebug审查元素</span>,查看如何<span style="color: rgba(255, 0, 0, 1)">解析html</span>,上一篇我已经讲过了,所以就不详细说了,大家不明白的可以<span style="color: rgba(255, 0, 0, 1)">看看代理ip上篇</span>。</span></p>
<p><span style="font-size: 18px">&nbsp;</span></p>
<p><span style="font-size: 18px">&nbsp; &nbsp; &nbsp; 下面咱们可以写代码了,由于咱们使用的是scrapy框架进行爬取,所以首先先<span style="color: rgba(255, 0, 0, 1)">生成scrapy工程</span>,在cmd中 输入<span style="color: rgba(255, 0, 0, 1)">scrapy startproject proxySpider_scrapy</span>,然后使用<span style="color: rgba(255, 0, 0, 1)">pycharm</span>打开。</span></p>
<p>&nbsp;</p>
<p><span style="font-size: 18px">工程结构如下:</span></p>
<p><img src="http://images2015.cnblogs.com/blog/899843/201605/899843-20160531094024992-1837526383.jpg"></p>
<p>&nbsp;</p>
<p><span style="font-size: 18px">  <span style="color: rgba(255, 0, 0, 1)">db包</span>中db_helper:实现的是mongodb的增删改查。和代理ip上篇<span style="color: rgba(255, 0, 0, 1)">增加了proxyId字段</span>。</span></p>
<p><span style="font-size: 18px">&nbsp;</span></p>
<p><span style="font-size: 18px">  <span style="color: rgba(255, 0, 0, 1)">detect包</span>中 detect_proxy:<span style="color: rgba(255, 0, 0, 1)">验证代理ip的可用性的线程</span></span></p>
<p><span style="font-size: 18px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  detect_manager: <span style="color: rgba(255, 0, 0, 1)">用来管理验证线程,监控线程状态</span></span></p>
<p><span style="font-size: 18px">&nbsp;</span></p>
<p><span style="font-size: 18px">  <span style="color: rgba(255, 0, 0, 1)">spiders包</span>中 proxySpider:主要实现<span style="color: rgba(255, 0, 0, 1)">爬虫的逻辑和html解析</span></span></p>
<p><span style="font-size: 18px">&nbsp;</span></p>
<p><span style="font-size: 18px">  <span style="color: rgba(255, 0, 0, 1)">items</span>:主要是描述了<span style="color: rgba(255, 0, 0, 1)">ip和port</span></span></p>
<p><span style="font-size: 18px">&nbsp;</span></p>
<p><span style="font-size: 18px">  <span style="color: rgba(255, 0, 0, 1)">pipelines</span>:里面主要是将<span style="color: rgba(255, 0, 0, 1)">爬取到的ip和port存储到数据库中</span></span></p>
<p><span style="font-size: 18px">&nbsp;</span></p>
<p><span style="font-size: 18px">  <span style="color: rgba(255, 0, 0, 1)">main</span>:主要是完成<span style="color: rgba(255, 0, 0, 1)">参数的判断和爬虫的启动</span>(咱们使用<span style="color: rgba(255, 0, 0, 1)">脚本来启动爬虫</span>不使用命令行)</span></p>
<p><span style="font-size: 18px">&nbsp;</span></p>
<p><span style="font-size: 18px">&nbsp;</span></p>
<p><span style="font-size: 18px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;还要说一下检测:我是用&nbsp;http://ip.chinaz.com/getip.aspx作为检测网址,只要使用<span style="color: rgba(255, 0, 0, 1)">代理访问不超时</span>,而且<span style="color: rgba(255, 0, 0, 1)">响应码为200</span>,咱们就认为是成功的代理。</span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>  <span style="font-size: 18px">接下来运行程序看看效果:</span></p>
<p><span style="font-size: 18px">  在windows下切换到工程目录,运行<span style="color: rgba(255, 0, 0, 1)">python main.py -h</span>,会看到我定义的<span style="color: rgba(255, 0, 0, 1)">使用说明和参数设置</span>。和上一篇基本上完全一样。</span></p>
<p>&nbsp;</p>
<p><img src="http://images2015.cnblogs.com/blog/899843/201605/899843-20160531094129805-1975722454.jpg"></p>
<p>&nbsp;</p>
<p>  <span style="font-size: 18px">接着运行<span style="color: rgba(255, 0, 0, 1)">python main.py -c 1 5</span> &nbsp;(意思是爬取<span style="color: rgba(255, 0, 0, 1)">1-5</span>页的ip地址):</span></p>
<p>&nbsp;</p>
<p><img src="http://images2015.cnblogs.com/blog/899843/201605/899843-20160531094214680-1509158935.jpg"></p>
<p>&nbsp;</p>
<p><span style="font-size: 18px">  这时候如果想验证ip的正确性:运行<span style="color: rgba(255, 0, 0, 1)">python main.py -t db</span></span></p>
<p>&nbsp;</p>
<p><img src="http://images2015.cnblogs.com/blog/899843/201605/899843-20160531094237227-867614594.jpg"></p>
<p>&nbsp;</p>
<p>  <span style="font-size: 18px">使用<span style="color: rgba(255, 0, 0, 1)">多线程验证</span>的速度非常快,我设置了<span style="color: rgba(255, 0, 0, 1)">5</span>个线程。两分钟不到,就验证结束。124个ip是可以使用的。</span></p>
<p>&nbsp;</p>
<p><img src="http://images2015.cnblogs.com/blog/899843/201605/899843-20160531094307586-1809076566.jpg"></p>
<p>&nbsp;</p>
<p><span style="font-size: 18px">  看一下mongodb数据库:</span></p>
<p>&nbsp;</p>
<p><img src="http://images2015.cnblogs.com/blog/899843/201605/899843-20160531094331039-519867717.jpg"></p>
<p>&nbsp;</p>
<p>&nbsp;  <span style="font-size: 18px">大家注意到那个<span style="color: rgba(255, 0, 0, 1)">proxyId字段</span>了吗?这个在我们进行<span style="color: rgba(255, 0, 0, 1)">多线程分段验证</span>的时候是很有用的。详细的使用,请看代码。</span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-size: 18px">当咱们<span style="color: rgba(255, 0, 0, 1)">下一篇讲解突破反爬虫</span>的时候就可以使用这些ip了。</span></p>
<p>&nbsp;</p>
<p><img src="http://images2015.cnblogs.com/blog/899843/201604/899843-20160412112100223-1243548045.jpg"></p>
<p>&nbsp;</p>
<p><span style="font-size: 18px"> 下面把<span style="color: rgba(255, 0, 0, 1)">解析和验证</span>的代码贴一下:</span></p>
<p>&nbsp;</p>
<div class="cnblogs_Highlighter">
<pre class="brush:python;gutter:true;">proxySpider.py:
#coding:utf-8
import scrapy
from proxySpider_scrapy.db.db_helper import DB_Helper
from proxySpider_scrapy.detect.detect_proxy import Detect_Proxy
from proxySpider_scrapy.detect.detect_manager import Detect_Manager
from proxySpider_scrapy.items import ProxyItem


'''
这个类的作用是将代理数据进行爬取
'''
class ProxySpider(scrapy.Spider):
    name = 'proxy'
    start_urls = ["http://www.xicidaili.com/nn/"]
    allowed_domains = []
    db_helper = DB_Helper()
    detecter = Detect_Manager(5)
    Page_Start = 1
    Page_End = 4
    headers = {
         'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
      'Accept-Language': 'en',
      'Referer':'http://www.xicidaili.com/'
    }

    def parse(self, response):
      '''
      解析出其中的ip和端口
      :param response:
      :return:
      '''
      trs = response.xpath('//tr[@class="odd" or @class=""]')
      for tr in trs:
            item = ProxyItem()
            tds = tr.xpath('./td/text()').extract()
            for td in tds:
                content = td.strip()
                if len(content)&gt;0:
                  if content.isdigit():
                        item['port'] = content
                        print 'ip:',item['ip']
                        print 'port:',item['port']
                        break
                  if content.find('.')!= -1:
                        item['ip'] = content


            yield item
      if self.Page_Start &lt; self.Page_End:
            new_url = self.start_urls+str(self.Page_Start)
            self.Page_Start += 1
            yield scrapy.Request(new_url,headers=self.headers,callback=self.parse)
</pre>
</div>
<p>  </p>
<p>&nbsp;</p>
<div class="cnblogs_Highlighter">
<pre class="brush:python;gutter:true;">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

from proxySpider_scrapy.spiders.proxySpider import ProxySpider


class ProxyPipeline(object):
    proxyId = 1 #设置一个ID号,方便多线程验证
    def process_item(self, item, spider):
      '''

      :param item:
      :param spider:
      :return:
      '''
      if spider.name == 'proxy':#需要判断是哪个爬虫

            proxySpider = ProxySpider(spider)
            proxy = {'ip':item['ip'],'port':item['port']}
            proxy_all = {'ip':item['ip'],'port':item['port'],'proxyId':self.proxyId}
            if proxySpider.db_helper.insert(proxy,proxy_all) == True:#插入数据
               self.proxyId += 1
            return item

      else:
            return item
</pre>
</div>
<p>  </p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div class="cnblogs_Highlighter">
<pre class="brush:python;gutter:true;">detect_manager.py:
#coding:utf-8
from threading import Thread
import time

from proxySpider_scrapy.db.db_helper import DB_Helper
from proxySpider_scrapy.detect.detect_proxy import Detect_Proxy

'''
定义一个管理线程,来管理产生的线程
'''
class Detect_Manager(Thread):

    def __init__(self,threadSum):
      Thread.__init__(self)
      sqldb = DB_Helper()#将序号重新恢复
      sqldb.updateID()
      self.pool =[]
      for i in range(threadSum):
            self.pool.append(Detect_Proxy(DB_Helper(),i+1,threadSum))


    def run(self):
      self.startManager()
      self.checkState()

    def startManager(self):
      for thread in self.pool:
            thread.start()

    def checkState(self):
      '''
      这个函数是用来检测线程的状态
      :return:
      '''
      now = 0
      while now &lt; len(self.pool):
            for thread in self.pool:
                if thread.isAlive():
                  now = 0
                  break
                else:
                  now+=1
            time.sleep(0.1)
      goodNum=0
      badNum =0
      for i in self.pool:

            goodNum += i.goodNum
            badNum += i.badNum
      sqldb = DB_Helper()#将序号重新恢复
      sqldb.updateID()
      print 'proxy good Num ---',goodNum
      print 'proxy bad Num ---',badNum
</pre>
</div>
<p>  </p>
<p>&nbsp;</p>
<div class="cnblogs_Highlighter">
<pre class="brush:python;gutter:true;">detect_proxy.py:
#coding:utf-8
import socket
from threading import Thread

import urllib

'''
这个类主要是用来检测代理的可用性
'''
class Detect_Proxy(Thread):


    url = 'http://ip.chinaz.com/getip.aspx'
    def __init__(self,db_helper,part,sum):
      Thread.__init__(self)
      self.db_helper = db_helper
      self.part = part#检测的分区
      self.sum = sum#检索的总区域

      self.counts = self.db_helper.proxys.count()
      socket.setdefaulttimeout(2)
      self.__goodNum = 0
      self.__badNum = 0

    @property
    def goodNum(self):
      return self.__goodNum

    @goodNum.setter
    def goodNum(self,value):
      self.__goodNum = value

    @property
    def badNum(self):
      return self.__badNum


    @badNum.setter
    def badNum(self,value):
      self.__badNum = value


    def run(self):

      self.detect()#开始检测


    def detect(self):
      '''
      http://ip.chinaz.com/getip.aspx作为检测目标
      :return:
      '''

      if self.counts &lt; self.sum:
            return

      pre = self.counts/self.sum
      start = pre * (self.part-1)
      end = pre * self.part
      if self.part == self.sum:#如果是最后一部分,结束就是末尾
            end = self.counts
      # print 'pre-%d-start-%d-end-%d'%(pre,start,end)

      proxys = self.db_helper.proxys.find({'proxyId':{'$gt':start,'$lte':end}})#大于start小于等于end,很重要


      for proxy in proxys:

            ip = proxy['ip']
            port = proxy['port']
            try:
                proxy_host ="http://ha:ha@"+ip+':'+port #随便添加了账户名和密码,只是为了防止填写账户名密码暂停的情况
                response = urllib.urlopen(self.url,proxies={"http":proxy_host})
                if response.getcode()!=200:
                  self.db_helper.delete({'ip':ip,'port':port})
                  self.__badNum += 1
                  print proxy_host,'bad proxy'
                else:
                  self.__goodNum += 1
                  print proxy_host,'success proxy'

            except Exception,e:
                print proxy_host,'bad proxy'
                self.db_helper.delete({'ip':ip,'port':port})
                self.__badNum += 1
                continue
</pre>
</div>
<p>  </p>
<pre><span style="font-size: 18px"><span style="color: rgba(255, 0, 0, 1)">完整的代码</span>我已经上传到<span style="color: rgba(255, 0, 0, 1)">github</span>上:</span><br><span style="font-size: 18px; color: rgba(255, 0, 0, 1)">https://github.com/qiyeboy/proxySpider_scrapy</span><br><br><br></pre>
<pre><span style="font-size: 18px">今天的分享就到这里,已经<span style="color: rgba(255, 0, 0, 1)">晚上12:15</span>了,如果大家觉得还可以呀,请<span style="color: rgba(255, 0, 0, 1)">继续支持我</span>。提前透露一下,<span style="color: rgba(255, 0, 0, 1)">下一篇会讲解突破反爬虫</span>。</span><br><br></pre>
<p><img src="http://images2015.cnblogs.com/blog/899843/201604/899843-20160424205951882-749864503.jpg"></p>
<p>&nbsp;</p>
<p><span style="font-size: 18px">&nbsp; 欢迎大家支持我公众号:&nbsp;&nbsp; &nbsp;</span></p>
<p><img src="http://images2015.cnblogs.com/blog/899843/201604/899843-20160412112303145-1979448153.jpg"></p>
<p>&nbsp;</p>
<p><span style="font-size: 18px">本文章属于原创作品,欢迎大家转载分享。<span style="color: rgba(255, 0, 0, 1)">尊重原创,转载请注明来自:七夜的故事&nbsp;http://www.cnblogs.com/qiyeboy/</span></span></p>
<pre></pre>
<pre><br><br></pre>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    本文章属于原创作品,欢迎大家转载分享,禁止修改文章的内容。尊重原创,转载请注明来自:七夜的故事 http://www.cnblogs.com/qiyeboy/<br><br>
来源:https://www.cnblogs.com/qiyeboy/p/5544917.html
頁: [1]
查看完整版本: Scrapy爬取美女图片第三集 代理ip(下)