参考:
【【2021最新版】Python 并发编程实战,用多线程、多进程、多协程加速程序运行】
import requests
import threading
urls = [
f"https://q.cnblogs.com/list/unsolved?page={page}"
for page in range(1, 50 + 1)
]
print(urls)
def craw(url):
r = requests.get(url)
print(url,len(r.text))
# 1-单线程for 循环
def single_thread():
print("single_thread begin!")
for url in urls:
craw(url)
print("single_thread end!!!!!")
# 2-多线程
def multi_thread():
print("multi_thread begin!")
threads = []
for url in urls:
threads.appe
thread.start()
for thread in threads:
thread.join()
print("multi_thread end!!!!!")
single_thread()
multi_thread()
【结果】