| 
 
	积分3625贡献 精华在线时间 小时注册时间2014-10-21最后登录1970-1-1 
 | 
 
 
 楼主|
发表于 2019-7-7 23:25:19
|
显示全部楼层 
| 本帖最后由 15195775117 于 2020-12-4 09:14 编辑 
 urllib.request打开url的案例:
 ----------------------------
 #作者:
 __author__ = '1039483898@qq.com'
 import urllib.request
 def linkBaidu():
 url='https://www.baidu.com'
 try:
 #打开url,timeout是超时时间设置
 response=urllib.request.urlopen(url,timeout=3)
 result=response.read().decode('utf-8')
 except Exception as e:
 print("网络地址错误")
 exit()
 #写文本
 with open('baidu.txt','w') as fp:
 fp.write(result)
 print("获取url信息:%s" %response.geturl())
 '''
 getcode()返回200表示成功
 404表示网页不存在
 503表示服务器暂不可用
 '''
 print("获取返回代码:%s" %response.getcode())
 print("获取返回信息: %s"%response.info())
 linkBaidu()
 输出结果:
 
   
 
 
 | 
 |