Python学习第十天

#!/usr/bin/python

#filename:urllib_test01.py


#import urllib2 # there is no urllib2 in python3.5

import urllib.request

html = urllib.request.urlopen("http://www.baidu.com").read()

#print (html)


def getdata(url):


  try :

    data=urllib.request.urlopen(url).read()

    z_data=data.decode("UTF-8")

    print(z_data)

  except Exception as e:

    print(e)

  return


getdata("http://www.baidu.com") #正确的url调用


getdata("https://www.baidui.com/") #错误的url调用


补充一版根据py3的教程学到的例子

#!/usr/bin/python

#filename:test14.py

f = open('test.txt','wb+')

from urllib.request import urlopen

with urlopen('http://www.baidu.com') as response:

for line in response:

#line = line.decode('utf-8')

#print(line)

f.write(line)

f.close


转载于:https://my.oschina.net/u/2329222/blog/637863

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值