首页 > 编程语言 > 详细

Python - 定时动态获取IP代理池,存放在文件中

时间:2018-09-01 18:45:03      阅读:200      评论:0      收藏:0      [点我收藏+]

定时功能通过module time + 死循环实现,因为time.sleep()会自动阻塞

get_ip_pool.py

"""
    @__note__: while True + time.sleep实现定时更新代理池文件(ip_pool)
    @__how_to_use: 1. 直接另开终端运行。(但终端会阻塞) 2. 后台运行。 Unix, 后台运行脚本: <script_name> &
"""
import requests
import time

PROXIES_URL = ‘‘


def get_ip_pool(url=PROXIES_URL):
    """ 根据URL构造代理池

    :param PROXIES_URL:
    :return: []
    """
    response = requests.get(url)
    with open(‘./ip_pool‘, ‘w‘) as f:
        f.write(response.text)
        f.flush()
    # time+死循环, 实现定时功能。
    time.sleep(10)
    print(‘get_ip_pool is done‘)


while True:
    get_ip_pool()

read_ip_pool.py

import random

def read_ip_pool():
    with open(‘IP_POOL‘, ‘r‘) as f:
        ip_list = f.readlines()
        ip = random.choice(ip_list).strip()
        # print(‘random ip is: ‘ + ip)
    print(‘read ip_pool is done‘)

# read_ip_pool()

Python - 定时动态获取IP代理池,存放在文件中

原文:https://www.cnblogs.com/allen2333/p/9571111.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!