python通过Keep-Alive保持IP不变,向多个页面发出请求,通过多线程实现并发控制
发布时间:2024年01月23日
import requests
import random
import requests.adapters
import threading
import time
targetUrlList = [
"https://httpbin.org/ip",
"https://httpbin.org/headers",
"https://httpbin.org/user-agent",
]
proxyHost = "t.16yun.cn"
proxyPort = "31111"
proxyUser = "username"
proxyPass = "password"
proxyMeta = f"http://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}"
proxies = {
"http": proxyMeta,
"https": proxyMeta,
}
last_request_time = 0
lock = threading.Lock()
def visit_url(url, i):
global last_request_time
with requests.session() as s:
with lock:
current_time = time.time()
delta_time = current_time - last_request_time
if delta_time < 0.2:
time.sleep(0.2 - delta_time)
last_request_time = time.time()
r = s.get(url, proxies=proxies)
print(f"第{i+1}个网址,结果如下:")
print(r.text)
threads = []
for i, url in enumerate(targetUrlList):
t = threading.Thread(target=visit_url, args=(url, i))
threads.append(t)
t.start()
for t in threads:
t.join()
文章来源:https://blog.csdn.net/Z_suger7/article/details/135775465
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:chenni525@qq.com进行投诉反馈,一经查实,立即删除!