内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

Python搭建爬蟲(chóng)代理池的方法-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關(guān)Python搭建爬蟲(chóng)代理池的方法,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)專(zhuān)注于企業(yè)成都全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站重做改版、廣靈網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)購(gòu)物商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為廣靈等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。

一、為什么要搭建爬蟲(chóng)代理池

在眾多的網(wǎng)站防爬措施中,有一種是根據(jù)ip的訪(fǎng)問(wèn)頻率進(jìn)行限制,即在某一時(shí)間段內(nèi),當(dāng)某個(gè)ip的訪(fǎng)問(wèn)次數(shù)達(dá)到一定的閥值時(shí),該ip就會(huì)被拉黑、在一段時(shí)間內(nèi)禁止訪(fǎng)問(wèn)。

應(yīng)對(duì)的方法有兩種:

1. 降低爬蟲(chóng)的爬取頻率,避免IP被限制訪(fǎng)問(wèn),缺點(diǎn)顯而易見(jiàn):會(huì)大大降低爬取的效率。

2. 搭建一個(gè)IP代理池,使用不同的IP輪流進(jìn)行爬取。

二、搭建思路

1、從代理網(wǎng)站(如:西刺代理、快代理、云代理、無(wú)憂(yōu)代理)爬取代理IP;

2、驗(yàn)證代理IP的可用性(使用代理IP去請(qǐng)求指定URL,根據(jù)響應(yīng)驗(yàn)證代理IP是否生效);

3、將可用的代理IP保存到數(shù)據(jù)庫(kù);

常用代理網(wǎng)站:西刺代理 、云代理 、IP海 、無(wú)憂(yōu)代理 、飛蟻代理 、快代理

三、代碼實(shí)現(xiàn)

工程結(jié)構(gòu)如下:

Python搭建爬蟲(chóng)代理池的方法

ipproxy.py

IPProxy代理類(lèi)定義了要爬取的IP代理的字段信息和一些基礎(chǔ)方法。

# -*- coding: utf-8 -*-
import re
import time
from settings import PROXY_URL_FORMATTER

schema_pattern = re.compile(r'http|https$', re.I)
ip_pattern = re.compile(r'^([0-9]{1,3}.){3}[0-9]{1,3}$', re.I)
port_pattern = re.compile(r'^[0-9]{2,5}$', re.I)

class IPProxy:
  '''
  {
    "schema": "http", # 代理的類(lèi)型
    "ip": "127.0.0.1", # 代理的IP地址
    "port": "8050", # 代理的端口號(hào)
    "used_total": 11, # 代理的使用次數(shù)
    "success_times": 5, # 代理請(qǐng)求成功的次數(shù)
    "continuous_failed": 3, # 使用代理發(fā)送請(qǐng)求,連續(xù)失敗的次數(shù)
    "created_time": "2018-05-02" # 代理的爬取時(shí)間
  }
  '''

  def __init__(self, schema, ip, port, used_total=0, success_times=0, continuous_failed=0,
         created_time=None):
    """Initialize the proxy instance"""
    if schema == "" or schema is None:
      schema = "http"
    self.schema = schema.lower()
    self.ip = ip
    self.port = port
    self.used_total = used_total
    self.success_times = success_times
    self.continuous_failed = continuous_failed
    if created_time is None:
      created_time = time.strftime('%Y-%m-%d', time.localtime(time.time()))
    self.created_time = created_time

  def _get_url(self):
    ''' Return the proxy url'''
    return PROXY_URL_FORMATTER % {'schema': self.schema, 'ip': self.ip, 'port': self.port}

  def _check_format(self):
    ''' Return True if the proxy fields are well-formed,otherwise return False'''
    if self.schema is not None and self.ip is not None and self.port is not None:
      if schema_pattern.match(self.schema) and ip_pattern.match(self.ip) and port_pattern.match(self.port):
        return True
    return False

  def _is_https(self):
    ''' Return True if the proxy is https,otherwise return False'''
    return self.schema == 'https'

  def _update(self, successed=False):
    ''' Update proxy based on the result of the request's response'''
    self.used_total = self.used_total + 1
    if successed:
      self.continuous_failed = 0
      self.success_times = self.success_times + 1
    else:
      print(self.continuous_failed)
      self.continuous_failed = self.continuous_failed + 1

if __name__ == '__main__':
  proxy = IPProxy('HTTPS', '192.168.2.25', "8080")
  print(proxy._get_url())
  print(proxy._check_format())
  print(proxy._is_https())

本文題目:Python搭建爬蟲(chóng)代理池的方法-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://www.rwnh.cn/article38/dosipp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開(kāi)發(fā)、電子商務(wù)域名注冊(cè)、軟件開(kāi)發(fā)動(dòng)態(tài)網(wǎng)站、服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都app開(kāi)發(fā)公司
湘潭县| 余干县| 水城县| 台北县| 霍城县| 茂名市| 扎鲁特旗| 新昌县| 大竹县| 堆龙德庆县| 潞城市| 绥阳县| 介休市| 东兰县| 灵宝市| 巩留县| 临颍县| 孝义市| 武鸣县| 芮城县| 日喀则市| 呼和浩特市| 彝良县| 治县。| 舞钢市| 扎兰屯市| 射洪县| 安远县| 万安县| 社旗县| 宜黄县| 华坪县| 隆尧县| 五原县| 上蔡县| 儋州市| 方正县| 鄢陵县| 从江县| 长春市| 汉寿县|