python3关于urllib中urlopen报错问题的解决

 00x0 前言

   最近更新了Python版本,准备写个爬虫,意外的发现urllib库中属性不存在urlopen,于是各种google,然后总结一下给出解决方案

  

00x1 问题的出现

   

AttributeError: 'module' object has no attribute 'urlopen'

00x2 问题的解决途径

    我们先来看下官方文档的解释:

[html] view plain  copy
 print ?
  1. a new urllib package was created. It consists of code from  
  2. urllib, urllib2, urlparse, and robotparser. The old  
  3. modules have all been removed. The new package has five submodules:  
  4. urllib.parse, urllib.request, urllib.response,  
  5. urllib.error, and urllib.robotparser. The  
  6. urllib.request.urlopen() function uses the url opener from  
  7. urllib2. (Note that the unittests have not been renamed for the  
  8. beta, but they will be renamed in the future.)  

  也就是说官方3.0版本已经把urllib2,urlp arse等五个模块都并入了urllib中,也就是整合了。

00x3 正确的使用方法
[python] view plain  copy
 print ?
  1. import urllib.request  
  2. url="http://www.baidu.com"  
  3. get=urllib.request.urlopen(url).read()  
  4. print(get)  
结果示意图:


  
其实也是可以换个utf-8的编码让读取出来的源码更正确的,但这已经是番外的不再提了。

转自:http://blog.csdn.net/pythonniu/article/details/51855035

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值