#导包
import threading
import requests
import time
#定义线程容器
threads = []
#定义计时器方法
#获取进入浏览器的当前时间
def get_time():
ms = time.ctime()
return ms
#定义抓取方法
def get_content(url):
r = requests.get(url)
print(r.status_code)
#定义多线程
for i in range(20):
mythread = threading.Thread(target=get_content,args=('https://www.pearvideo.com',))
threads.append(mythread)
if __name__ == "__main__":
print('开始于%s' % get_time())
# 同步请求
# for x in range(20):
# get_content('https://www.pearvideo.com')
#开始异步多线程请求
for t in threads:
#守护线程
t.setDaemon(True)
#执行
t.start()
#打印每一个线程的执行时间
print('这一个线程执行到%s' % get_time())
#阻塞一下主线程
t.join()
print('结束于%s' % get_time())
多线程爬虫的一个小demo
最新推荐文章于 2020-07-27 10:39:42 发布