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并需要一些作业来巩固你的知识,我可以给你一些建议。你可以考虑以下几个方面来进行自学作业: 1. 完成Sololearn提供的Python 3教程。这个教程包含92章和275个相关测验,可以帮助你系统地学习和掌握Python编程语言。你可以按照章节的顺序逐步学习,并在每个章节结束后完成相关的测验来检验自己的理解和掌握程度。\[1\] 2. 在Udemy上注册一个帐户,并参加他们提供的Python课程。Udemy提供了从初学者到专家级别的Python课程,你可以选择适合自己的课程来学习。完成整个课程后,你还可以获得Python结业证书。你可以尝试使用Python创建一些小游戏,比如纸牌游戏,来练习你的编码技巧。\[2\]\[3\] 3. 参考其他在线资源,如Python官方文档、Stack Overflow等,来解决一些实际的编程问。你可以尝试解决一些编程挑战,如编写一个简单的计算器程序、实现一个简单的数据分析任务等。这样可以帮助你将所学的知识应用到实际问中,并提升你的编程能力。 总之,通过完成教程、参加课程和解决实际问,你可以不断提升自己的Python编程技能。记得在学习过程中保持坚持和实践,这样才能更好地掌握Python编程。祝你学习顺利! #### 引用[.reference_title] - *1* *2* *3* [30个在线Python自学网站,再也不用到处找资料了](https://blog.csdn.net/nnn0245/article/details/126555892)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值