本次作业要求:
习题一:已知列表 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 threading
import time
def test(p):
time.sleep(0.1)
print p
ts=[]
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 threading
info = [1,2,3,4,55,233]
def test(p):
print p
threadset = []
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 problem
import urllib
from BeautifulSoup import BeautifulSoup
def 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 title
def 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_status
def 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)