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

定时功能通过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()

猜你喜欢

转载自www.cnblogs.com/allen2333/p/9571111.html