定时功能通过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()
原文地址:https://www.cnblogs.com/allen2333/p/9571111.html
时间: 2024-10-11 23:10:45