欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

解决python3 urllib中urlopen报错的问题

时间:2021-07-29 07:43:00|栏目:Python代码|点击:

前言

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

问题的出现

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

问题的解决途径

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

a new urllib package was created. It consists of code from 
urllib, urllib2, urlparse, and robotparser. The old 
modules have all been removed. The new package has five submodules: 
urllib.parse, urllib.request, urllib.response, 
urllib.error, and urllib.robotparser. The 
urllib.request.urlopen() function uses the url opener from 
urllib2. (Note that the unittests have not been renamed for the 
beta, but they will be renamed in the future.) 

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

正确的使用方法

import urllib.request 
url="http://www.baidu.com" 
get=urllib.request.urlopen(url).read() 
print(get) 

结果示意图:

 

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

总结

上一篇:浅谈Pytorch 定义的网络结构层能否重复使用

栏    目:Python代码

下一篇:Python实现求解括号匹配问题的方法

本文标题:解决python3 urllib中urlopen报错的问题

本文地址:http://www.codeinn.net/misctech/162422.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有