Python自学-第16次作业
来源:互联网 发布:js点击按钮跳指定div 编辑:程序博客网 时间:2024/06/04 19:15
本次作业要求:
习题一:已知列表 info = [1,2,3,4,55,233]
生成6个线程对象,每次线程输出一个值,最后输出:”the end”。
习题二:已知列表 urlinfo =
[‘http://www.sohu.com‘,’http://www.163.com‘,’http://www.sina.com‘]
用多线程的方式分别打开列表里的URL,并且输出对应的网页标题和内容。习题三:已知列表 urlinfo =
[‘http://www.sohu.com‘,’http://www.163.com‘,’http://www.sina.com‘]
用多线程的方式分别打开列表里的URL,输出网页的http状态码。
不多说,代码如下:
#coding=utf-8'''import threadingimport timedef test(p): time.sleep(0.1) print pts=[]for i in xrange(0,15): th = threading.Thread(target=test,args=[i]) th.start() ts.append(th)for i in ts: i.join()print 'hello end!!'a = threading.Thread(target=test)b = threading.Thread(target=test)a.start()b.start()a.join()b.join()'''import threadinginfo = [1,2,3,4,55,233]def test(p): print pthreadset = []def func1(info): for i in xrange(6): th = threading.Thread(target=test,args=[info[i]]) th.start() threadset.append(th) for i in xrange(6): threadset[i].join() print 'ok'func1(info)#second problemimport urllibfrom BeautifulSoup import BeautifulSoupdef get_title(url): html = urllib.urlopen(url).read() # print html #m = re.search("<title>.*</title>", html) #print m.group() # 这里输出结果 <title>Apple</title> #print m.group().strip("</title>") # 问题应该出现在这个正则 soup = BeautifulSoup(html) #print soup title = soup.find('title') print titledef func2(urllist): for i in urllist: th =threading.Thread(target=get_title,args=[i]) th.start() print 'ok'func2(['http://www.sohu.com','http://www.csdn.net','http://www.sina.com'])#third problem#输出网页的http状态码#已知列表 urlinfo = ['http://www.sohu.com','http://www.163.com','http://www.sina.com']#用多线程的方式分别打开列表里的URL,输出网页的http状态码def get_status(url): res=urllib.urlopen(url) page_status=res.getcode() print 'page status is %d' % page_status return page_statusdef fun3(urllist): for i in urllist: th = threading.Thread(target=get_status,args=[i]) th.start() print 'output web page http response code is ok'urlinfo = ['http://www.sohu.com','http://www.163.com','http://www.sina.com']print 'begin third problem'fun3(urlinfo)
1 0
- Python自学-第16次作业
- Python自学-第11次作业
- Python自学-第12次作业
- python自学-第八次作业
- Python自学--第九次作业
- 第7次作业
- 第6 次作业
- 第5次作业
- 第11次作业
- 第X次作业
- 第11次作业
- 第3次作业
- 第4次作业
- 第5次作业
- 第6次作业
- 第7次作业
- 第9次作业
- 第8次作业
- ssl协商以及秘钥交换
- C# 进程管理器
- 2017.02.24 Javascript 面向对象编程2--继承
- android面试热点:webview使用及与js数据交互
- LeetCode : Intersection of Two Arrays
- Python自学-第16次作业
- android studio上传项目到github
- input输入框回车事件响应
- 欢迎使用CSDN-markdown编辑器
- 如何保证kafka 的消息机制
- FreeRTOS操作系统移植
- 继承
- 建立一个文件,在文件中输入学生信息,查找文件,输出学生的信息
- 持续集成 部署 交付