一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - python如何基于redis實(shí)現(xiàn)ip代理池

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

2020-04-17 10:24Maple_feng Python

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
 
 
# 隨機(jī)獲取一個(gè)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錯(cuò)誤失效就刪除
    remove_ip(ip,redis_conn)
 
 
scheduler = BlockingScheduler()
scheduler.add_job(func=aps_detection_ip, args=('檢測(cè)循環(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=('檢測(cè)循環(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("檢測(cè)循環(huán)任務(wù)")

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

原文鏈接:https://www.cnblogs.com/angelyan/p/12157374.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费在线视频网站 | 久久WWW免费人成一看片 | 暖暖视频免费观看视频中国.韩剧 | 热辣小秘书办公室 | 成人久久18免费网站入口 | 精品国产在天天线在线麻豆 | ai换脸杨颖被啪在线观看 | 四虎永久免费地址在线观看 | 无人影院免费观看 | 青草免费在线 | 我的漂亮朋友在线观看全集免费 | 毛片在线网址 | 91亚洲一区二区在线观看不卡 | 歪歪私人影院成人毛片 | 99ri国产在线| 九九99香蕉在线视频美国毛片 | 99视频全部免费 | 久久艹综合 | 紧缚束缚调教丨vk | 国产极品麻豆91在线 | 日韩在线视频免费观看 | 精品欧美一区二区三区四区 | 国产成人精品高清不卡在线 | 激情婷婷综合久久久久 | 国产精品一区二区久久 | 忘忧草研究院一二三 | 欧美午夜精品 | 6080伦理久久精品亚洲 | 菠萝视频5正版在线观看 | 亚洲国产精品综合欧美 | 欧美精品国产一区二区 | 国产麻豆传媒在线观看 | 成人午夜影院在线观看 | 女教师巨大乳孔中文字幕免费 | 福利视频久久 | 暖暖免费观看高清在线 | 我不卡影院手机在线观看 | 国产精品久久久久久久久ktv | 亚洲精品色婷婷在线影院麻豆 | 丝袜足液精子免费视频 | 肥胖女人一级毛片 |