Python自学-第16次作业

本次作业要求:

习题一:已知列表 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)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
python作业 一、Turtle创意大PK 自拟题目,完成一个利用Python程序的创意绘图,采用turtle库绘图为主,不少于50行代码,可选采用其他库。 (滑稽绘制) 二、程序练习 2.1 问题描述(10分) 人们常常提到"一万小时定律",就是不管你做什么事情,只要坚持一万小时,应该都可以成为该领域的专家。那么,10000小时是多少年多少天呢? 2.2 问题描述(10分)0380031003800341590145037657 编写计算从n到m和的函数‬,函数名为sum(n,m)‬,函数返回为n到m所有数据的和‬,使用该函数计算输入数据x,y之间所有数据的和。 2.3 问题描述(15分) 编写函数judgeTri(a,b,c),判断以参数a,b,c的为边长能否构成三角形并判断三角形的形状;若是锐角三角形,返回R;若是直角三角形,返回Z;若是钝角三角形,返回D;若三边长不能构成三角形,返回ERROR。 2.4 问题描述(15分) 用户输入一个字符串,分别统计其中小写字母、大写字母、数字、空格和其他字符的个数,并在一行内输出小写字母、大写字母、数字、空格和其他字符的个数。 2.5 问题描述(20分) 程序的功能: (1) 使用随机库功能,生成一个包含10个不重复且小于200的正整数列表ls1,输出ls1。‬ (2) 使用列表排序方法,对ls1按奇数在前偶数在后,并且奇数之间的相对顺序不变,偶数之间的相对顺序也不变进行排序,再输出ls1。‬ (3) 使用列表排序方法,对ls1按元素字符长度降序进行排序,输出ls1。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值