最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python如何基于redis實現(xiàn)ip代理池

 更新時間:2020年01月17日 10:32:53   作者:Maple_feng  
這篇文章主要介紹了python如何基于redis實現(xiàn)ip代理池,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

這篇文章主要介紹了python如何基于redis實現(xiàn)ip代理池,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

使用apscheduler庫定時爬取ip,定時檢測ip刪除ip,做了2層檢測,第一層爬取后放入redis——db0進行檢測,成功的放入redis——db1再次進行檢測,確保獲取的代理ip的可用性

import requests, redis
import pandas
import random

from apscheduler.schedulers.blocking import BlockingScheduler
import datetime
import logging

db_conn = redis.ConnectionPool(host="*.*.*.*", port=6379, password="123456")
redis_conn_0 = redis.Redis(connection_pool=db_conn, max_connections=10,db=0)
redis_conn_1 = redis.Redis(connection_pool=db_conn, max_connections=10,db=1)


# 刪除redis數(shù)據(jù)庫里的ip
def remove_ip(ip,redis_conn):
  redis_conn.zrem("IP", ip)
  print("已刪除 %s..." % ip)


# 獲取redis數(shù)據(jù)庫里一共有多少ip
def get_ip_num(redis_conn):
  num = redis_conn.zcard("IP")
  return num


# 獲取ip的端口
def get_port(ip,redis_conn):
  port = redis_conn.zscore("IP", ip)
  port = str(port).replace(".0", "")
  return port


# 添加ip和端口到數(shù)據(jù)庫里
def add_ip(ip, port,redis_conn):
  # nx: 不要更新已有的元素??偸翘砑有碌脑?只有True,F(xiàn)alse
  redis_conn.zadd("IP", {ip: port}, nx=55)
  print("已添加 %s %s...ok" % (ip, port))


# 列出所有的ip
def get_all_ip(redis_conn):
  all_ip = redis_conn.zrange("IP", 0, -1)
  return all_ip


# 隨機獲取一個ip
def get_random_ip(redis_conn):
  end_num = get_ip_num(redis_conn)
  num = random.randint(0, end_num)
  random_ip = redis_conn.zrange("IP", num, num)
  if not random_ip:
    return "",""
  random_ip = str(random_ip[0]).replace("b", '').replace("'", "")
  port = get_port(random_ip,redis_conn)
  return random_ip, port


# 獲取代理ip
def spider_ip(x,redis_conn):
  print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), x)
  for p in range(1, 20):
    res = pandas.read_html("http://www.89ip.cn/index_{}.html".format(p))
    # print(res)
    # print(type(res[0]))
    for i in range(len(res[0])):
      ip = res[0].iloc[i, 0]
      port = res[0].iloc[i, 1]
      print("ip", ip)
      print("port", port)
      add_ip(str(ip), str(port),redis_conn)


logging.basicConfig(level=logging.INFO,
          format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
          datefmt='%Y-%m-%d %H:%M:%S',
          filename='log1.txt',
          filemode='a')


def aps_detection_ip(x,redis_conn):
  print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), x)
  res=get_random_ip(redis_conn)
  ip=res[0]
  port=res[1]
  try:
    requests.get("http://www.baidu.com",proxies={'https':'{ip}:{port}'.format(ip=ip,port=port)})
    print("可用",ip,port,res)
    if redis_conn!=redis_conn_1:
      add_ip(str(ip), str(port), redis_conn_1)
  except Exception:
    # ip錯誤失效就刪除
    remove_ip(ip,redis_conn)


scheduler = BlockingScheduler()
scheduler.add_job(func=aps_detection_ip, args=('檢測循環(huán)任務(wù)0',redis_conn_0), trigger='interval', seconds=3, id='aps_detection_ip_task0',max_instances=10)
scheduler.add_job(func=spider_ip, args=('獲取循環(huán)任務(wù)0',redis_conn_0), trigger='interval', seconds=60*60*2, id='spider_ip_task0',max_instances=10)

scheduler.add_job(func=aps_detection_ip, args=('檢測循環(huán)任務(wù)1',redis_conn_1), trigger='interval', seconds=3, id='aps_detection_ip_task1',max_instances=10)

scheduler._logger = logging

# scheduler.start()
if __name__ == '__main__':
  # print(get_ip_num())
  # spider_ip("獲取循環(huán)任務(wù)")
  scheduler.start()
  # aps_detection_ip("檢測循環(huán)任務(wù)")

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Opencv實現(xiàn)眼睛控制鼠標(biāo)的實踐

    Opencv實現(xiàn)眼睛控制鼠標(biāo)的實踐

    本文主要介紹了Opencv實現(xiàn)眼睛控制鼠標(biāo)的實踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Python中Collections模塊的Counter容器類使用教程

    Python中Collections模塊的Counter容器類使用教程

    Counter是Python標(biāo)準(zhǔn)庫提供的一個非常有用的容器,可以用來對序列中出現(xiàn)的各個元素進行計數(shù),下面就來一起看一下Python中Collections模塊的Counter容器類使用教程
    2016-05-05
  • 關(guān)于Python網(wǎng)絡(luò)爬蟲框架scrapy

    關(guān)于Python網(wǎng)絡(luò)爬蟲框架scrapy

    這篇文章主要介紹了關(guān)于Python網(wǎng)絡(luò)爬蟲框架scrapy,爬蟲框架是實現(xiàn)爬蟲功能的一個軟件結(jié)構(gòu)和功能組件的集合,需要的朋友可以參考下
    2023-04-04
  • Python并行庫joblib之delayed函數(shù)與Parallel函數(shù)詳解

    Python并行庫joblib之delayed函數(shù)與Parallel函數(shù)詳解

    這篇文章主要介紹了Python并行庫joblib之delayed函數(shù)與Parallel函數(shù)詳解,Joblib就是一個可以簡單地將Python代碼轉(zhuǎn)換為并行計算模式的軟件包,它可非常簡單并行我們的程序,從而提高計算速度,需要的朋友可以參考下
    2023-08-08
  • 解決新django中的path不能使用正則表達式的問題

    解決新django中的path不能使用正則表達式的問題

    今天小編就為大家分享一篇解決新django中的path不能使用正則表達式的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • 如何使用?Python?Pandas?更新行和列

    如何使用?Python?Pandas?更新行和列

    這篇文章主要介紹了使用?Python?Pandas?更新行和列的方法,在整篇文章中,我們將使用我們現(xiàn)在要創(chuàng)建的數(shù)據(jù)框,這將使大家了解更新數(shù)據(jù)操作,在此之后,大家可以將這些方法應(yīng)用于自己的數(shù)據(jù),需要的朋友可以參考下
    2023-03-03
  • 如何在Python中引入和使用瀏覽器驅(qū)動

    如何在Python中引入和使用瀏覽器驅(qū)動

    本文介紹了如何在Python中引入和使用瀏覽器驅(qū)動,主要步驟包括安裝Selenium庫、下載并配置瀏覽器驅(qū)動路徑、編寫Python代碼啟動瀏覽器以及結(jié)束操作后關(guān)閉瀏覽器
    2025-01-01
  • Python中try excpet BaseException(異常處理捕獲)的使用

    Python中try excpet BaseException(異常處理捕獲)的使用

    本文主要介紹了Python中try excpet BaseException(異常處理捕獲)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Python打包為exe詳細(xì)教程

    Python打包為exe詳細(xì)教程

    今天給大家介紹如何用Python打包exe,文中有非常詳細(xì)的教程,對正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • pandas 透視表中文字段排序方法

    pandas 透視表中文字段排序方法

    今天小編就為大家分享一篇pandas 透視表中文字段排序方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11

最新評論

大方县| 城固县| 大关县| 沙洋县| 进贤县| 井研县| 股票| 南汇区| 鹤山市| 都匀市| 阿拉尔市| 麻城市| 石台县| 新密市| 广丰县| 寿光市| 石城县| 祁门县| 济宁市| 正阳县| 崇礼县| 廊坊市| 襄垣县| 阿克陶县| 绥德县| 沁阳市| 密山市| 故城县| 巴马| 八宿县| 株洲市| 康保县| 海阳市| 栖霞市| 岗巴县| 金溪县| 巧家县| 通榆县| 合江县| 陈巴尔虎旗| 汤阴县|