如何用多线程改进这个下载程序?

fnzh0003 fnzh0003 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我写了一个程序,下载http://www.network-theory.co.uk/docs/pytut/里面的在线教程,
import time
import urllib
import lxml.html
import os
time1=time.time()
os.mkdir('/tmp/python')
down='http://www.network-theory.co.uk/docs/pytut/'
file=urllib.urlopen(down).read()
root=lxml.html.fromstring(file)
tnodes = root.xpath("//div[@class='main']//ul/li/a")
for x in tnodes:
url='http://www.network-theory.co.uk/docs/pytut/'+x.get('href')
name=x.text
myfile=open('/tmp/python/'+name,'a')
page=urllib.urlopen(url).read()
myfile.write(page)
myfile.close()
time2=time.time()
print time2-time1
我使用上面的程序,测试了下载时间,564秒,好慢,可能使用多线程,可以加快下载.
请问,如何用多线程进行改写??
...全文
评论 7
收藏 已收藏
分享
返回顶部
fnzh0003
脚本语言 37,738 成员 34,208 内容