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
原创粉丝点击