import time,requestes
from tomorrow import threads
@threads(10)#使用装饰器,这个函数异步执行
def download(url):
return requests.get(url)
def main():
start = time.time()
urls = [
'https://pypi.org/project/tomorrow/0.2.0/',
'https://www.cnblogs.com/pyld/p/4716744.html',
'http://www.xicidaili.com/nn/10',
'http://baidu.com',
'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879',
]
responses = [download(i) for i in urls]
end = time.time()
print("Time: %f seconds" % (end - start))
if __name__=="__main__":
main()
不使用多线程:
使用多线程:

本文通过一个具体的Python示例介绍了如何利用多线程来提高网页内容的抓取效率。示例中定义了一个下载函数并使用了装饰器实现异步执行,对比了使用与未使用多线程的情况。

1178





